多任务时不调用 traitCollectionDidChange

traitCollectionDidChange not called when multitasking

我正在努力采用多任务处理来支持应用程序的拆分视图,但我发现当应用程序在右侧时未调用 traitCollectionDidChange

有人知道吗?

您是否尝试过 viewWillTransitionToSize 方法?这用于通知容器它的视图大小即将改变。

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")
    }
}

根据 UITraitEnvironment 的文档:

系统在iOS界面环境发生变化时调用该方法。根据您的应用程序的需要,在视图控制器和视图中实现此方法,以响应此类更改。例如,当 iPhone 从纵向旋转到横向时,您可以调整视图控制器的子视图的布局。此方法的默认实现为空。 在您的实现开始时,调用 super 以确保视图层次结构中较高的界面元素有机会首先调整其布局。

您可以在 ViewController 中覆盖 traitCollectionDidChange

不过

traitCollectionDidChange 当您从一种拆分视图模式更改为另一种时调用。例如,从 50/50 视图到 33/66。因此当您进入或退出多任务模式时,它 NOT 被调用。

如果您需要处理所有事件,包括进入和退出多任务模式,请改用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)
    }

如果您希望在 AFTER 转换完成后调用您的代码:

-(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)
    }];
}

如果还有人怀疑那是重点:

// 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
    }];
}

还有一个方法traitCollectionDidChange:,但只有当水平大小class实际改变时才会调用。例如,如果您的应用在拆分视图模式下从右侧呈现,则当用户更改分隔符位置时不会调用 traitCollectionDidChange:。但是,如果您的应用程序从左侧呈现,它将始终以纵向模式调用,并以横向模式进行转换 (50/50) <-> (66/33)

在视图控制器中,我们应该调用

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

在 orientation/multi window 视图更改时收到通知。