iOS 8 Orientation Change Detection

后端 未结 2 1142
挽巷
挽巷 2021-01-03 23:35

Running on iOS 8, I need to change the UI when rotating my app.

Currently I am using this code:

-(BOOL)shouldAutorotate
{
    UIDeviceOrientation ori         


        
相关标签:
2条回答
  • 2021-01-04 00:12

    Timur Kuchkarov is correct, but I'll post the answer since I missed his comment the first time I checked this page.

    The iOS 8 method of detecting orientation change (rotation) is implementing the following method of the view controller:

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    {
        // Do view manipulation here.
        [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    }
    

    Note: The controller's view has not yet transitioned to that size at this time, so be careful if your sizing code relies on the view's current dimensions.

    0 讨论(0)
  • 2021-01-04 00:26

    The viewWillTransitionToSize:withTransitionCoordinator: method is called immediately before the view has transitioned to the new size, as Nick points out. However, the best way to run code immediately after the view has transitioned to the new size is to use a completion block in the method:

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
        [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            // your code here
        }];
    }
    

    Thanks to the this answer for the code and to Nick for linking to it in his comment.

    0 讨论(0)
提交回复
热议问题