gpt4 book ai didi

cocoa - 在 iOS 6 上使用 Storyboard 处理方向/旋转

转载 作者:行者123 更新时间:2023-12-03 16:10:11 27 4
gpt4 key购买 nike

所以......我刚刚花了整个下午的时间来处理这个问题,看到了各种帖子并尝试了很多方法来让它发挥作用,但没有运气。我现在已经尽可能将其带回一个简单的案例,但它似乎仍然不起作用。

在这个简单的场景中,我有一个 Storyboard,仅包含一个 UINavigationController 和一个初始 View Controller : enter image description here

看过所有关于 IOS 6 中自动旋转方法更改的帖子后,我创建了 UINavigationController 的子类,并在该子类和第一个 View Controller 上实现了 shouldAutorotate 和supportedInterfaceOrientations 方法:

导航 Controller :

// NavController.h
#import <UIKit/UIKit.h>

@interface NavController : UINavigationController

@end

// NavController.m
#import "NavController.h"

@interface NavController ()

@end

@implementation NavController

- (BOOL) shouldAutorotate {
return YES;
}

- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}

@end

第一 Controller

// FirstController.h
#import <UIKit/UIKit.h>

@interface FirstController : UIViewController

@end

// FirstController.m
#import "FirstController.h"

@interface FirstController ()

@end

@implementation FirstController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate {
return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}

@end

项目设置指定应支持所有旋转: enter image description here

这一切似乎都没有任何效果......!当我在模拟器中运行它时,我得到:

enter image description here

enter image description here

请..!我在这里缺少什么?这真让我抓狂!我希望至少能让这个简单的案例工作起来,这样我就有希望让我的相当大的应用程序工作!

最佳答案

如果您计划启用或禁用所有 View Controller 的旋转,则无需子类化 UINavigationController。 而是使用:

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

在你的 AppDelegate 中。

如果您计划支持应用程序中的所有方向,但支持父 View Controller (UINavigationController 堆栈)上的不同方向,则应使用

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

与父 View Controller 中的以下方法结合使用。

    - (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations

但是,如果您计划在同一导航堆栈中的不同子 ViewController 中使用不同的方向设置(就像我一样),您需要检查导航堆栈中的当前 ViewController。

我在 UINavigationController 子类中创建了以下内容:

    - (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
int interfaceOrientation = 0;

if (self.viewControllers.count > 0)
{
id viewController;
DLog(@"%@", self.viewControllers);
for (viewController in self.viewControllers)
{
if ([viewController isKindOfClass:([InitialUseViewController class])])
{
interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
else if ([viewController isKindOfClass:([MainViewController class])])
{
interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
else
{
interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown;
}
}
}
return interfaceOrientation;
}

由于您无法再从子 ViewController 控制所呈现 View Controller 的旋转设置,因此您必须以某种方式拦截当前导航堆栈中的 View Controller 。这就是我所做的:)。希望有帮助!

关于cocoa - 在 iOS 6 上使用 Storyboard 处理方向/旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12553921/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com