iOS 6旋转不工作 [英] iOS 6 rotation not working
问题描述
我改写了UINavigationController来实现这些方法:
- (BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
现在从我的第一个视图,我模态地呈现这个MSNavigationPaneViewController 这里就像一个Facebook侧滑动标签栏控制器。
我需要在这里显示的我的视图控制器中的一个才显示横向,而应用程序的其余视图只有纵向。
所有在其他视图控制器已添加:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return UIInterfaceOrientationMaskPortrait;
}
并且在我想添加的景观中添加:
- (BOOL)shouldAutorotate
{
return UIInterfaceOrientationMaskLandscape;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
但是我的所有视图都旋转。
如果我在这些其他视图中将 shouldRotate
更改为NO,那么它们保持在纵向,我想要的视图是可以旋转的,但是我不能让它自己旋转。
此外,如果我回到另一个具有 shouldRotate = NO;
的视图,一旦旋转,则不能旋转回到纵向。
我已经在这里工作了几个小时,不能工作。
EDIT -----
我现在有这个一半工作,并开始一个新的问题,以获得自动旋转工作这里
看起来你对在shouldAutorotate中返回的东西感到困惑。使用以下代码作为示例。
由于iOS正在调用应用程序的shouldAutorotate以响应加速度计中的事件,因此它已经知道新的方向;如果你的应用程序回答是,iOS可以检查当前的方向对照支持的列表,并提出一个决定,而无需你的应用程序查询当前的方向。
// iOS 6
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
此外,您可以让呈现模态视图控制器的视图控制器通知它的旋转。使用:presentViewController:animated:completion:呈现视图控制器。
如果添加 application:supportedInterfaceOrientationsForWindow:
,请务必遵守以下代码指南。该文档声明,如果在VC上实现 supportedInterfaceOrientations
,它应该覆盖应用程序委托。但是,人们已经注意到,如何将 rootViewController
添加到主窗口中会有所不同。
使用:
window.rootViewController = viewController
而不是:
[window addSubview:viewController.view];
I have overridden my UINavigationController to implement these methods:
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
Now from my first view, I modally present this MSNavigationPaneViewController here which is like a Facebook side sliding tab bar controller.
I need 1 of my view controllers that are displayed within here to be displayed Landscape only while the rest of the app's views are Portrait only.
So in all my other View Controllers I have added:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return UIInterfaceOrientationMaskPortrait;
}
and in the one that I want Landscape i've added:
- (BOOL)shouldAutorotate
{
return UIInterfaceOrientationMaskLandscape;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
However all my views rotate.
If I change shouldRotate
to NO on these other views then they stay in Portrait and the view I want to be Landscape can be rotated, however I can't get it to rotate itself.
Also, once rotated if I go back to another view that has shouldRotate = NO;
then it can't be rotated back to Portrait.
I've been at this for hours now and can't get it to work.
Thanks
EDIT -----
I have this half working now and have started a new question to get the auto rotation working here
It seems you are confusing what to return in shouldAutorotate. Use the following code as an example.
Since iOS is making a call into your app's shouldAutorotate in response to an event from the accelerometer, it already knows the new orientation; if your app answers 'YES`, iOS could then check the current orientation against the list of supported ones, and come up with a decision without your app querying for the current orientation.
// iOS 6
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Also, You can let the view controller that presents your modal view controller inform it of rotation. Use: presentViewController:animated:completion: to present the view controller. presentModalViewController:animated: is deprecated if by chance you are using it.
If you add application:supportedInterfaceOrientationsForWindow:
, make sure to follow the code guideline below. The documentation states that if you implement supportedInterfaceOrientations
on a VC it should override the app delegate. However, people have noticed it makes a difference as to how you add the rootViewController
to the main window.
Use:
window.rootViewController = viewController
instead of:
[window addSubview:viewController.view];
这篇关于iOS 6旋转不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!