如何改变应用程序的方向而不改变iPhone应用程序的设备方向

我想改变应用程序的方向而不改变iPhone应用程序中的设备方向。 我想以编程方式将我的视图从portraid模式更改为景观模式。

还想知道这是否会被苹果商店接受?

谢谢

现在我得到了其他解决scheme,如下所示

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 

当你在这个时候添加这行的时候会出现一个警告,为了除去这个警告,只需要在你的实现文件中添加下面的代码。

 @interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere) - (void) setOrientation:(UIInterfaceOrientation)orientation; @end 

然后在波纹pipe法中,如果需要的话就写这个代码。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); } 

但现在想知道这是否被苹果应用程序商店接受?

谢谢

使用此线以编程方式更改方向…

 [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 

而且当你在这个时候添加这一行的时候会出现一个警告,为了移除这个警告,只需要在你的实现文件中添加下面的代码。

 @interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere) - (void) setOrientation:(UIInterfaceOrientation)orientation; @end 

然后在波纹pipe法中,如果需要的话就写这个代码。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations // return NO; return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); } 

我希望这可以帮助你..

🙂

添加一个类variables

 Bool isInLandsCapeOrientation; 

在viewDidLoad中

设置这个标志

 isInLandsCapeOrientation = false; 

添加以下function

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (!isInLandsCapeOrientation) { return (UIInterfaceOrientationIsPortrait(interfaceOrientation)); }else { return (UIInterfaceOrientationIsLandscape(interfaceOrientation)); } } 

要改变从纵向到横向的方向,让它发生在button动作上

  - (IBAction)changeOrientationButtonPressed:(UIButton *)sender { isInLandsCapeOrientation = true; UIViewController *viewController = [[UIViewController alloc] init]; [self presentModalViewController:viewController animated:NO]; [self dismissModalViewControllerAnimated:NO]; } 

这对我来说很好。

要将方向portraid模式更改为景观模式,请使用此代码

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); } 

使用此代码以编程方式更改方向…

 [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 

如果你只想在景观中改变特定的视图,那么你可以在viewDidLoad中尝试下面的内容

  float angle = M_PI / 2; CGAffineTransform transform = CGAffineTransformMakeRotation(angle); [ [self.view] setTransform:transform]; 

文档描述的方向属性是只读的,所以如果它的工作,我不知道你可以依靠它在未来的工作(除非苹果做了聪明的事情,并改变这种情况;迫使方向的变化,无论如何用户目前正在拿着他们的设备,这是一个明显的function需求)。

作为替代scheme,在viewDidLoad中插入的以下代码将成功(并且有点好奇地)强制定位(假设您已经修改了shouldAutorotateToInterfaceOrientation):

 if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { UIWindow *window = [[UIApplication sharedApplication] keyWindow]; UIView *view = [window.subviews objectAtIndex:0]; [view removeFromSuperview]; [window addSubview:view]; } 

显然,如果用户当前正在以纵向方向握住他们的设备(因此大概您的shouldAutorotateToInterfaceOrientation被设置为仅横向(landscape)),并且如果用户将他们的设备保持为纵向模式,则该例程将其转换为横向(landscape)。 如果您的shouldAutorotateToInterfaceOirentation设置为仅用于纵向,则只需将UIDeviceOrientationIsPortraitUIDeviceOrientationIsLandscape交换即可。

出于某种原因,从主窗口中删除视图,然后重新添加它迫使它查询shouldAutorotateToInterfaceOrientation并正确设置方向。 鉴于这不是一个苹果认可的方法,也许应该避免使用它,但它适用于我。 你的旅费可能会改变。 但是这也涉及其他技术。 检查SO讨论