How to constrain autorotation to a single orientation for some views, while allowing all orientations on others?

后端 未结 5 1322
孤街浪徒
孤街浪徒 2020-12-01 03:44

This question is about iOS device rotation and multiple controlled views in a UINavigationController. Some views should be constrained to portrait orientation, and

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 04:16

    DO NOT USE THIS HACK, APPLE WILL REJECT THE APP BASED ON THE USE OF 'PRIVATE API'

    For the sake of reference, I will leave my answer here, but the use of private API will not slip past the review board. I learnt something today :D As @younce quoted the Apple docs correctly, what I want cannot be achieved with the UINavigationController.

    I had two options. First, I could have written my own navigation controller substitute, with all the horrors that one would have encountered while doing it. Secondly, I could have hacked the rotation into the view controllers, using an undocumented feature of UIDevice called setOrientation:animated:

    Because the second was temptingly easy, I went for that one. This is what I did. You'll need a category to suppress compiler warnings about the setter not existing:

    @interface UIDevice (UndocumentedFeatures) 
    -(void)setOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated;
    -(void)setOrientation:(UIInterfaceOrientation)orientation;
    @end
    

    Then you need to check for the supported orientations on viewWillAppear:. Next to the UIDevice methods used here, you could also force portrait orientation by presenting a modal view controller, but that will happen instantly and not animated, so this is my preferred way:

    -(void)viewWillAppear:(BOOL)animated {
        UIDevice *device = [UIDevice currentDevice];
        UIDeviceOrientation realOrientation = device.orientation;
    
        if ([self shouldAutorotateToInterfaceOrientation:realOrientation]) {
            if (realOrientation != [UIApplication sharedApplication].statusBarOrientation) {
    
                // Resetting the orientation will trigger the application to rotate
                if ([device respondsToSelector:@selector(setOrientation:animated:)]) {
                    [device setOrientation:realOrientation animated:animated];
                } else {
                    // Yes if Apple changes the implementation of this undocumented setter,
                    // we're back to square one.
                }
            }
        } else if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait]) {
            if ([device respondsToSelector:@selector(setOrientation:animated:)]) {
    
                // Then set the desired orientation
                [device setOrientation:UIDeviceOrientationPortrait animated:animated];
    
                // And set the real orientation back, we don't want to truly mess with the iPhone's balance system.
                // Because the view does not rotate on this orientation, it won't influence the app visually.
                [device setOrientation:realOrientation animated:animated];
            }
        }
    }
    

    The trick is to always keep the internal device orientation to the 'real' orientation of the device. If you start changing that, the rotation of your app will be out of balance.

    But as I know now, this is just a sure way to get your app rejected. So option number two is just a bad option. Rewrite that NavigationController, or just have all your views support the same orientation set.

    Cheers, EP.

提交回复
热议问题