【问题标题】:Why can't I force landscape orientation when use UINavigationController?为什么我在使用 UINavigationController 时不能强制横向?
【发布时间】:2013-01-17 11:19:35
【问题描述】:

我发现许多问题迫使 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;

【问题讨论】:

    标签: ios objective-c uinavigationcontroller uiinterfaceorientation


    【解决方案1】:

    最好的方法是创建扩展 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)作为您的根视图控制器的导航控制器。

    @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 代码如下。

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

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

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

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

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

    【讨论】:

    • 我只有 1UINavigationController 用于所有 UIViewController。以编程方式创建自定义 navigationController 的合适位置在哪里。请指导一些代码!
    • 请检查您是否可以。
    • 谢谢!但我的 UINavigation 只支持横向。当推送到下一个 UIViewController 时,它不支持纵向:github.com/lequysang/TestOrient
    • 我的目标:root(landscape) -> next UIViewController(portrait + Landscape)
    • 请检查我上传到我的github上的代码github.com/sunilpandey/DifferentOrientation
    【解决方案2】:

    您需要转到您的 plist 文件并设置方向。您也可以对项目文件执行此操作。 plist 方向设置将覆盖所有内容。

    在您的 plist 中,您可以将两个方向选项设置为横向按钮左侧和横向按钮右侧。

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多