Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

traitCollectionDidChange not called when multitasking

I'm working on an adopting multitasking to support split view for app, but I find the traitCollectionDidChange not called when app is on the right.

Does anyone have idea about this?

like image 792
RayChen Avatar asked Dec 07 '15 07:12

RayChen


3 Answers

You may override traitCollectionDidChange in your ViewController.

However.

traitCollectionDidChange called when you change from one split view mode to another. For example from 50/50 view to 33/66. It so NOT called when you enter multitasking mode or exit it.

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

// 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 called AFTER the transition 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)
    }];
}
like image 116
Mike Keskinov Avatar answered Sep 20 '22 13:09

Mike Keskinov


Have you tried the viewWillTransitionToSize method? This is used to notify the container that the size of its view 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")
    }
}
like image 21
Rashwan L Avatar answered Sep 21 '22 13:09

Rashwan L


If anyone still doubt that 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
    }];
}

Also there is a method traitCollectionDidChange:, but it will be called only when horizontal size class actually changed. For example, if your app presenting from right side in split view mode, traitCollectionDidChange: will not be called when user changed separator position. But if your app presenting from left, it will be called always in portrait mode, and in landscape for transitions (50/50) <-> (66/33)

like image 21
Roman Truba Avatar answered Sep 18 '22 13:09

Roman Truba