Zac*_*man 18 iphone objective-c uinavigationcontroller ios ios5
我的应用程序主要是肖像,但有一个视图需要横向方向.
我的观点包含在UINavigationController中,显然这是导致此问题的原因.
除了一个之外的所有UIViewControllers都有:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)
需要Landscape的UIViewController有这样的:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Run Code Online (Sandbox Code Playgroud)
现在,当用户到达横向UIViewController时会发生什么,它以纵向显示.然后,用户可以旋转他们的手机,并按照我想要的方式显示在风景中(锁定到风景).然后用户继续前进到肖像UIViewController,同样的事情发生:它从横向开始,然后他们旋转他们的手机,它再次成为肖像(并锁定到肖像).
似乎UIViewControllers之间允许方向锁定,但是以某种方式阻止了自动旋转/以编程方式更改方向.
如何强制手机更新到正确的方向?
有一个临时解决方案:我可以检测设备的方向并显示一条消息,要求他们旋转设备,如果它不正确,但这不是最佳的.
Gun*_*nds 26
我对我的一个申请有同样的要求!
幸运的是我找到了解决方案!
为了保持主视图控制器的风景,无论从哪个方向弹出/推动,我都做了以下事情:(在viewWillAppear :)
//set statusbar to the desired rotation position
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
//present/dismiss viewcontroller in order to activate rotating.
UIViewController *mVC = [[[UIViewController alloc] init] autorelease];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
Run Code Online (Sandbox Code Playgroud)
在sdk 3.2.5 ios 5.0.1上进行了PST.
PS在iOS 8之前的答案导致一些屏幕闪烁,而且 - 它不稳定(在某些情况下它不再适用于我.)因此,根据我的需要,我将代码更改为:(ARC)
//set statusbar to the desired rotation position
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
[self.navigationController presentViewController:[UIViewController new] animated:NO completion:^{
dispatch_after(0, dispatch_get_main_queue(), ^{
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
});
}];
//I added this code in viewDidDissapear on viewController class, which will be popped back.
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助!
Can*_*pus 19
这可能有所帮助.在适当的情况下,您可以在出现时调用以下方法.例如in-viewWillAppear:animated
attemptRotationToDeviceOrientation 尝试将所有窗口旋转到设备的方向.
+ (void)attemptRotationToDeviceOrientation
Run Code Online (Sandbox Code Playgroud)
讨论
某些视图控制器可能希望使用特定于应用程序的条件来确定其实现的shouldAutorotateToInterfaceOrientation:方法的返回值.如果您的视图控制器执行此操作,当这些条件发生更改时,您的应用程序应调用此类方法.系统立即尝试旋转到新方向.只要每个相关视图控制器在其shouldAutorotateToInterfaceOrientation:方法的实现中返回YES,就会发生旋转.
可用性
适用于iOS 5.0及更高版本. http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html
| 归档时间: |
|
| 查看次数: |
17625 次 |
| 最近记录: |