gpt4 book ai didi

ios - 为什么我在使用 UINavigationController 时不能强制横向?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:20 26 4
gpt4 key购买 nike

我发现很多问题强制 UIViewController 默认横向: How to force a UIViewController to Portrait orientation in iOS 6试试这个:

- (BOOL)shouldAutorotate
{
return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}

但是当我在 UINavigationController 中使用 UIViewController 时,我无法强制横向。请帮助!

*工作不顺利

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[navControl setNavigationBarHidden:YES];
self.window.rootViewController = navControl;

[self.window makeKeyAndVisible];
return YES;
}

*工作正常:

self.window.rootViewController = self.viewController;

最佳答案

最好的方法是创建扩展 UINavigationController 并在扩展类中编写您的方向函数。

类声明:

@interface MyNavigationControllerViewController : UINavigationController

@property(nonatomic, assign) UIInterfaceOrientation orientation;
@property(nonatomic, assign) NSUInteger supportedInterfaceOrientatoin;
@end

MyNavigationController 的实现

@implementation MyNavigationController

@synthesize supportedInterfaceOrientatoin = _supportedInterfaceOrientatoin;

@synthesize orientation = _orientation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
_orientation = UIInterfaceOrientationLandscapeLeft;
}
return self;
}

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

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
return _supportedInterfaceOrientatoin;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return self.orientation;
}

@end

并使用像 MyNavigationController 这样的扩展作为你的 rootviewcontroller 的导航 Controller 。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) MyNavigationControllerViewController *myNavController;

- (void) reloadAppDelegateRootViewControllerLandscape;
- (void) reloadAppDelegateRootViewController;
@end

因此您的应用程序委托(delegate) didfinishlounchingwithoptions 代码如下。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

self.myNavController = [[MyNavigationController alloc] initWithRootViewController:self.viewController];
[self.myNavController setNavigationBarHidden:YES];
self.window.rootViewController = self.myNavController;

[self.window makeKeyAndVisible];
return YES;
}

唯一可以为具有相同导航 Controller 的两个不同 View 提供不同方向的方法是重新加载导航 Controller 本身。所以如果你添加两个方法

- (void) reloadAppDelegateRootViewController{
[[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
[(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationPortrait];
[(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskAll];
[[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}

- (void) reloadAppDelegateRootViewControllerLandscape{
[[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
[(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationLandscapeLeft];
[(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskLandscape];
[[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}

并在推送和弹出 View 后调用这些函数。

注意:- 我不知道这是好方法还是坏方法。

关于ios - 为什么我在使用 UINavigationController 时不能强制横向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14658268/

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