UITraitCollection类,用于更新Size类 [英] UITraitCollection Class for Updating the Size Class

查看:166
本文介绍了UITraitCollection类,用于更新Size类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用UITraitCollection类更新Size类约束吗?这是更新约束的最佳实践吗?

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

我已经完成了UITraitCollection,但是不知道如何区分人像和风景?

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

推荐答案

听起来您需要基于iPad方向的其他布局. 如果仅需要调整约束值,则可以检查UITraitCollectionhorizontalSizeClassverticalSizeClass属性.可以在Apple文档中找到 UIUserInterfaceSizeClass .我不能保证这是最佳做法,但是我看不出有什么问题.另一种检查UITraitCollection的方法是检查UIInterfaceOrientationIsPortrait,如下面的代码片段所示.

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.

更复杂的场景要求对风景和肖像使用完全不同的约束.您可以通过编程方式处理添加这些约束,也可以使用其他尺寸类别添加约束,然后为基于方向的每个尺寸类别的约束创建IBOutletCollection.

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.

例如,我使用wAnyhRegular设置我的纵向iPad布局,然后使用wRegularhAny设置我的横向iPad布局. (尽管您可能希望将wRegular/hRegular用作方向布局之一,因为当您检查UITraitCollection时,iPad注册为wRegular/hRegular.希望下面的代码演示我的操作方法:

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;

我的肖像约束可以在下面看到.我的风景也有3个约束.

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

然后我按如下所述应用约束(未显示,viewDidLoad执行_needsiPadConstraintsApplied = YES;):

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.

这篇关于UITraitCollection类,用于更新Size类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆