<tfoot id='dlU8G'></tfoot>
<legend id='dlU8G'><style id='dlU8G'><dir id='dlU8G'><q id='dlU8G'></q></dir></style></legend>
      <bdo id='dlU8G'></bdo><ul id='dlU8G'></ul>

    <small id='dlU8G'></small><noframes id='dlU8G'>

      <i id='dlU8G'><tr id='dlU8G'><dt id='dlU8G'><q id='dlU8G'><span id='dlU8G'><b id='dlU8G'><form id='dlU8G'><ins id='dlU8G'></ins><ul id='dlU8G'></ul><sub id='dlU8G'></sub></form><legend id='dlU8G'></legend><bdo id='dlU8G'><pre id='dlU8G'><center id='dlU8G'></center></pre></bdo></b><th id='dlU8G'></th></span></q></dt></tr></i><div id='dlU8G'><tfoot id='dlU8G'></tfoot><dl id='dlU8G'><fieldset id='dlU8G'></fieldset></dl></div>

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

      时间:2023-05-17
    2. <legend id='k6ZEP'><style id='k6ZEP'><dir id='k6ZEP'><q id='k6ZEP'></q></dir></style></legend>
      • <bdo id='k6ZEP'></bdo><ul id='k6ZEP'></ul>
        <i id='k6ZEP'><tr id='k6ZEP'><dt id='k6ZEP'><q id='k6ZEP'><span id='k6ZEP'><b id='k6ZEP'><form id='k6ZEP'><ins id='k6ZEP'></ins><ul id='k6ZEP'></ul><sub id='k6ZEP'></sub></form><legend id='k6ZEP'></legend><bdo id='k6ZEP'><pre id='k6ZEP'><center id='k6ZEP'></center></pre></bdo></b><th id='k6ZEP'></th></span></q></dt></tr></i><div id='k6ZEP'><tfoot id='k6ZEP'></tfoot><dl id='k6ZEP'><fieldset id='k6ZEP'></fieldset></dl></div>

          <tbody id='k6ZEP'></tbody>
      • <small id='k6ZEP'></small><noframes id='k6ZEP'>

            • <tfoot id='k6ZEP'></tfoot>
              1. 本文介绍了为什么我在使用 UINavigationController 时不能强制横向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我发现很多问题迫使 UIViewController 默认为横向:如何在 iOS 中强制 UIViewController 为纵向6试试这个:

                I find many question to force UIViewController to landscape as default: How to force a UIViewController to Portrait orientation in iOS 6 And try this:

                - (BOOL)shouldAutorotate
                {
                    return YES;
                }
                
                -(NSUInteger)supportedInterfaceOrientations
                
                {
                    return UIInterfaceOrientationMaskLandscape;
                }
                
                - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
                {
                    return UIInterfaceOrientationLandscapeLeft;
                }
                

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

                But when I use UIViewController inside UINavigationController, I can't force to landscape.Please help!

                *工作不正常

                - (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 并在扩展类中编写定位函数.

                Best way to do this is the create extend UINavigationController and write your orientation function inside the extended class.

                类声明:

                @interface MyNavigationControllerViewController : UINavigationController
                
                @property(nonatomic, assign) UIInterfaceOrientation orientation;
                @property(nonatomic, assign) NSUInteger supportedInterfaceOrientatoin;
                @end
                

                MyNavigationController 的实现

                Implementation of 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 的导航控制器.

                and use your extended like MyNavigationController as navigation controller to your rootviewcontroller.

                @interface AppDelegate : UIResponder <UIApplicationDelegate>
                
                @property (strong, nonatomic) UIWindow *window;
                
                @property (strong, nonatomic) ViewController *viewController;
                @property (strong, nonatomic) MyNavigationControllerViewController *myNavController;
                
                - (void) reloadAppDelegateRootViewControllerLandscape;
                - (void) reloadAppDelegateRootViewController;
                @end
                

                所以您的应用程序委托 didfinishlounchingwithoptions 代码如下.

                So your application delegate didfinishlounchingwithoptions code will be as follow.

                - (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;
                }
                

                您可以为具有相同导航控制器的两个不同视图提供不同方向的唯一方法是重新加载导航控制器本身.所以如果你添加两个方法

                Only way you can provide different orientation for two different view with same navigation controller isto reload the navigation controller itself. So if you add two methods

                - (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];
                }
                

                并在推送和弹出视图后调用这些函数.

                and call these function after pushing and pop views.

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

                Note:- I don't know whether it is a good way or bad way.

                这篇关于为什么我在使用 UINavigationController 时不能强制横向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:防止双击 UITabBarController 时自动 popToRootViewController 下一篇:如何在 Swift 编程中获得基于导航的模板功能

                相关文章

                <small id='apWQs'></small><noframes id='apWQs'>

                <i id='apWQs'><tr id='apWQs'><dt id='apWQs'><q id='apWQs'><span id='apWQs'><b id='apWQs'><form id='apWQs'><ins id='apWQs'></ins><ul id='apWQs'></ul><sub id='apWQs'></sub></form><legend id='apWQs'></legend><bdo id='apWQs'><pre id='apWQs'><center id='apWQs'></center></pre></bdo></b><th id='apWQs'></th></span></q></dt></tr></i><div id='apWQs'><tfoot id='apWQs'></tfoot><dl id='apWQs'><fieldset id='apWQs'></fieldset></dl></div>
              2. <legend id='apWQs'><style id='apWQs'><dir id='apWQs'><q id='apWQs'></q></dir></style></legend>
                  • <bdo id='apWQs'></bdo><ul id='apWQs'></ul>
                1. <tfoot id='apWQs'></tfoot>