iOS6:supportedInterfaceOrientations不工作(调用但接口仍然旋转)

ane*_*yzm 50 iphone objective-c ios ios6

在我的应用程序中,我有多个视图,一些视图需要支持纵向和横向,而其他视图只需要支持纵向.因此,在项目摘要中,我已经选择了所有方向.

以下代码用于在iOS 6之前禁用给定视图控制器上的横向模式:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)

由于在iOS6中不推荐使用shouldAutorotateToInterfaceOrientation,我将以上内容替换为:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMask.Portrait;
}
Run Code Online (Sandbox Code Playgroud)

当视图出现时我会正确调用此方法(我可以设置断点以确保这一点),但是界面仍然会旋转到横向模式,而不管我是否仅为纵向模式返回蒙版.我究竟做错了什么?

似乎目前无法构建一个每个视图具有不同方向要求的应用程序.它似乎只遵循项目摘要中指定的方向.

Mar*_*tin 69

如果您正在使用的UINavigationController作为根窗口控制器,这将是 shouldAutorotatesupportedInterfaceOrientations这将被调用.

请注意,如果您使用的是UITabBarController,依此类推.

所以要做的是子类化导航/ tabbar控制器并覆盖它的shouldAutorotate&supportedInterfaceOrientations方法.

  • 好的,但是这样所有推送的视图都会有相同的旋转规则,对吗?我想要一些视图旋转,一些视图不旋转. (5认同)

小智 30

尝试在AppDelegate.m中更改此代码

//   self.window.rootViewController = self.navigationController;

    [window setRootViewController:navigationController];
Run Code Online (Sandbox Code Playgroud)

这是完整的答案

shouldAutorotateToInterfaceOrientation未在iOS 6中调用

XD


Pav*_*vel 20

在我的情况下,我有UINavigationController和我的视图控制器.我不得不继承UINavigationController,为了只支持Portrait,添加以下方法:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
Run Code Online (Sandbox Code Playgroud)

所以在UINavigationController子类中,我需要检查当前topViewController支持哪个方向.

- (NSUInteger)supportedInterfaceOrientations
{
    return [[self topViewController] supportedInterfaceOrientations];
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*nes 15

我发现的一件事是,如果你有一个旧的应用程序仍然在做

[window addSubView:viewcontroller.view];  //This is bad in so may ways but I see it all the time...
Run Code Online (Sandbox Code Playgroud)

您需要将其更新为:

[window setRootViewController:viewcontroller]; //since iOS 4
Run Code Online (Sandbox Code Playgroud)

一旦你这样做,方向应该开始再次工作.


Phi*_*hil 14

对于iOS6,最好的方法是在Ray Wenderlich团队的"iOS6 By Tutorials"中注明 - http://www.raywenderlich.com/并且在大多数情况下比UINavigationController的子类化要好.

我正在使用带有故事板的iOS6,其中包含一个UINavigationController设置为初始视图控制器.

//AppDelegate.m - 遗憾的是,此方法在iOS6之前不可用

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

if(self.window.rootViewController){
    UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
    orientations = [presentedViewController supportedInterfaceOrientations];
}

return orientations;
}
Run Code Online (Sandbox Code Playgroud)

//MyViewController.m - 返回您想要为每个UIViewController支持的任何方向

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)


Cau*_*ity 10

如其他人所说,如果您正在使用UINavigationController并且想要自定义各种视图,那么您需要将UINavigationController子类化并确保您拥有以下两个组件:

@implementation CustomNavigationController

// -------------------------------------------------------------------------------
//  supportedInterfaceOrientations:
//  Overridden to return the supportedInterfaceOrientations of the view controller
//  at the top of the navigation stack.
//  By default, UIViewController (and thus, UINavigationController) always returns
//  UIInterfaceOrientationMaskAllButUpsideDown when the app is run on an iPhone.
// -------------------------------------------------------------------------------
- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations]; 
}

// -------------------------------------------------------------------------------
//  shouldAutorotate
//  Overridden to return the shouldAutorotate value of the view controller
//  at the top of the navigation stack.
//  By default, UIViewController (and thus, UINavigationController) always returns
//  YES when the app is run on an iPhone.
// -------------------------------------------------------------------------------
- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}
Run Code Online (Sandbox Code Playgroud)

然后在任何只有肖像的视图中,您将包括:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

而在任何观点中,这一切都是颠倒的:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

48689 次

最近记录:

9 年,2 月 前