ios - 如何将一个 View Controller 方向单独强制/锁定到 landscapeRight?

标签 ios objective-c iphone

我尝试将 shouldAutorotate 设置为 false supportedInterfaceOrientations 到 UIInterfaceOrientationLandscapeRight

-(BOOL)shouldAutorotate{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationLandscapeRight;
}

最佳答案

supportedInterfaceOrientations 就够了,去掉 shouldAutorotate 就可以了。 或者你可以这样做。

NavigationController.m

@interface NavigationController : UINavigationController

@end

@implementation NavigationController

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:animated];
    [self refreshOrientation];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    id vc = [super popViewControllerAnimated:animated];
    [self refreshOrientation];
    return vc;
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    id vcs = [super popToViewController:viewController animated:animated];
    [self refreshOrientation];
    return vcs;
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    id vcs = [super popToRootViewControllerAnimated:animated];
    [self refreshOrientation];
    return vcs;
}

- (void)refreshOrientation {
    UIViewController *vc = [UIViewController new];
    [UIViewController attemptRotationToDeviceOrientation];
    [self presentViewController:vc animated:NO completion:nil];
    [vc dismissViewControllerAnimated:NO completion:nil];
}
@end

in ViewController implement this method

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

关于ios - 如何将一个 View Controller 方向单独强制/锁定到 landscapeRight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41569848/

相关文章:

ios - viewDidLoad多线程问题

iOS7 : Navigation bar like dropbox

ios - 如何让 UIAnimation 一次执行一个(迭代 for 循环),而不是一次执行全部?

objective-c - 在 Objective C 方法中创建 UIButton

React Native 中的 iOS 启动屏幕

iphone - 获取常量值 : unrecognized selector sent to instance error

ios - XML 到对象 Objective-C

iphone - iOS 应用因 "Could not load NIB in bundle"而崩溃

iPhone OpenGL ES 2.0 奇怪的图案在 COLLADA 文件中渲染球体纹理

iphone - 如何加载带有附加 View Controller 的 xib 文件?