Rotation methods deprecated, equivalent of 'didRotateFromInterfaceOrientation'?

IosIphoneObjective CIos8

Ios Problem Overview


I'm trying to implement the new viewWillTransitionToSize method which has been introduced in iOS 8 (all other rotation methods have been deprecated). I'd like to know what the equivalent of didRotateFromInterfaceOrientation is now as there are a number of clean up tasks we need to perform and I can't see a block that we can assign to UIViewControllerTransitionCoordinator in order to be called when 'transition' to a new size finishes. Thanks.

Ios Solutions


Solution 1 - Ios

Okay found it, just have to use the animateAlongsideTransition:completion: method on the passed UIViewControllerTransitionCoordinator.

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{   
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        // do whatever
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
    { 
    
    }];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

Solution 2 - Ios

The Swift Version of the answer by strange

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    
    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in
        
        let orient = UIApplication.sharedApplication().statusBarOrientation
        
        switch orient {
        case .Portrait:
            println("Portrait")
            // Do something
        default:
            println("Anything But Portrait")
            // Do something else
        }
        
        }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
            println("rotation completed")
    })
    
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}

Solution 3 - Ios

iOS 10.3 & Swift 3

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
        
        coordinator.animate(alongsideTransition: { (_) in
            let orient = newCollection.verticalSizeClass
            
            switch orient {
            case .compact:
                print("Lanscape")///Excluding iPads!!!
                
            default:
                print("Portrait")
            }
        }, completion: { _ in
            print("rotation completed")
        })
        
        super.willTransition(to: newCollection, with: coordinator)
    }

Solution 4 - Ios

The accepted answer in Swift 3:

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animate(alongsideTransition: { (_) in
        let orient = UIApplication.shared.statusBarOrientation
            
        switch orient {
        case .portrait:
            print("Portrait")
        // Do something
        default:
            print("Anything But Portrait")
            // Do something else
        }
    }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
      print("rotation completed")
    })
        
    super.willTransition(to: newCollection, with: coordinator)
}

It works fine for me 

Solution 5 - Ios

Since the question was: what was the equivalent of didRotateFromInterfaceOrientation

I thought I'd contribute the code below:

@implementation ViewController
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    if (previousTraitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular) {
        NSLog(@"User has rotated to landscape");
    } else if (previousTraitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
        NSLog(@"User has rotated to portrait");
    }
}
@end

I was testing on an iPhone in the simulator, but my print statements won't get run if I test using the iPad since the traitsCollection won't change.

This is strange because this is exactly what Apple recommends:

- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
    [super traitCollectionDidChange: previousTraitCollection];
    if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
        || self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {
        // your custom implementation here
    }
}

Solution 6 - Ios

[[UIApplication sharedApplication] statusBarOrientation] is deprecated in iOS9 you have to test against UITraitCollection for various devices.

  override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    
    if newCollection.containsTraitsInCollection(UITraitCollection(verticalSizeClass: .Regular)) {
      ...
    }
  }

Solution 7 - Ios

On the Ipad there is no trait collection change so here is how you detect the rotation from start and completion. Here is the Swift 5 syntax:

override func viewWillTransition(to size: CGSize, with coordinator: 
UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    coordinator.animate(alongsideTransition: { [unowned self] _ in
        
        self.view.backgroundColor = UIColor.blue
        print("rotation in progress")
        
    }) { [unowned self] _ in
        self.view.backgroundColor = UIColor.green
        print("rotation complete")
   
       
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstrangeView Question on Stackoverflow
Solution 1 - IosstrangeView Answer on Stackoverflow
Solution 2 - IosDogCoffeeView Answer on Stackoverflow
Solution 3 - IosMike GlukhovView Answer on Stackoverflow
Solution 4 - Iosj_gonferView Answer on Stackoverflow
Solution 5 - IosNYC Tech EngineerView Answer on Stackoverflow
Solution 6 - IosYannickView Answer on Stackoverflow
Solution 7 - IosAvi PogrowView Answer on Stackoverflow