TraitCollectionDidChange not called when multitasking

I am working on adopting multitasking to support a shared view for an application. But I believe that traitCollectionDidChange is not called when the application is on the right. Does anyone have an idea about this?

+6
source share
5 answers

Have you tried the viewWillTransitionToSize method? This is used to notify the container that its size is about to change.

Objective-c

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 

Swift

 func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { coordinator.animateAlongsideTransition(nil, completion: { if UIDevice.currentDevice().orientation.isLandscape.boolValue { print("landscape") } else { print("portrait") } } 
+6
source

You can override traitCollectionDidChange in ViewController .

But.

traitCollectionDidChange called upon transition from one split mode to another. For example, from 50/50 to 33/66. It causes NOT when you enter multitasking mode or exit it.

If you need to handle all events , including entering and exiting multitasking mode, use viewWillTransitionToSize:withTransitionCoordinator: :

 // put this in your ViewController code -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; // TODO: put your code here (runs BEFORE transition complete) } 

If you want your code to be called AFTER , a transitive compelete:

 -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { // TODO: put your code here (runs AFTER transition complete) }]; } 
+9
source

If someone still doubts this is the point:

 // This method called every time user changes separator position or when user rotates device -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { // Always call super in those methods [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; // Before this call your layout is old, status bar orientation will return value before rotation [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) { // Code here will be executed during transform. Status bar orientation is new, your view size already changed (in general). // Setup required animations or custom views transitions } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { // Split view transition complete or device rotated }]; } 

There is also a traitCollectionDidChange: method, but it will only be called when the horizontal-size class has actually changed. For example, if your application is displayed on the right side in split view, traitCollectionDidChange: will not be called when the user changes the position of the separator. But if your application is displayed on the left, it will always be called in portrait mode and in the landscape for transitions (50/50) <-> (66/33)

+2
source

According to the UITraitEnvironment documentation:

The system calls this method when the iOS interface environment changes. Implement this method in the view of the controllers and views in accordance with the needs of your applications in order to respond to such changes. For example, you can adjust the location of the sub-zones of the view controller when the iPhone rotates from portrait to landscape. By default, this method is empty. At the beginning of your implementation, call super to make sure that the interface elements above in the view hierarchy have the ability to first adjust their layout.

0
source

In the view controller, we should call

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {

to be notified when the orientation / view of multiple windows changes.

0
source

All Articles