UITraitCollection Class for Updating the Size Class

此生再无相见时 提交于 2019-12-12 05:36:59

问题


Shall i use UITraitCollection Class for Updating the Size Class constraints? Is this Best Practice to update the Constraints?

I have gone through the UITraitCollection, but don't know how to differentiate portrait and Landscape?


回答1:


It sounds like you want a different layout based on iPad orientation. If adjusting constraint values is all you need to do, you can check the UITraitCollection's horizontalSizeClass and verticalSizeClass properties. The size class properties values can be found at the Apple documentation for UIUserInterfaceSizeClass. I can't vouch for this being best practice, but I don't see anything wrong with it. An alternative to checking the UITraitCollection would be to check UIInterfaceOrientationIsPortrait as seen below in the code snippet.

A more complex scenario requires the use of completely different constraints for landscape vs portrait. You could handle adding those constraints programmatically, or you can use a different size class to add the constraints and then create an IBOutletCollection for the constraints for each size class that are based on the orientation.

For example, I used wAnyhRegular to setup my portrait iPad layout and then used wRegularhAny to setup my landscape iPad layout. (Although you may want to use wRegular/hRegular as one of your orientation layouts since iPad registers as wRegular/hRegular when you check the UITraitCollection. Hopefully the code below demonstrates how I went about it:

@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *iPadPortraitConstraints;
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *iPadLandscapeConstraints;

My portrait constraints can be seen below. My landscape has 3 constraints as well.

I then apply the constraints as noted below (not shown, viewDidLoad executes _needsiPadConstraintsApplied = YES;):

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self applyiPadConstraints];
}

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

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    //  Size Classes does not support differentiating between iPad Portrait & Landscape.
    //  Signal that the iPad rotated so we can manually change the constraints.
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        _needsiPadConstraintsApplied = YES;
    }
}
- (void)applyiPadConstraints {

    if (_needsiPadConstraintsApplied) {

        if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
            [NSLayoutConstraint deactivateConstraints:self.iPadLandscapeConstraints];
            [NSLayoutConstraint activateConstraints:self.iPadPortraitConstraints];

        } else {
            [NSLayoutConstraint deactivateConstraints:self.iPadPortraitConstraints];
            [NSLayoutConstraint activateConstraints:self.iPadLandscapeConstraints];
        }

        _needsiPadConstraintsApplied = NO;
    }
}

And finally, you may find this exploration of size classes helpful.



来源:https://stackoverflow.com/questions/29650332/uitraitcollection-class-for-updating-the-size-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!