区分iPad方向与尺寸等级 [英] Distinguish iPad orientation with size classes

查看:160
本文介绍了区分iPad方向与尺寸等级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助iOS 8,Apple推出了尺寸类来处理应用程序的布局。不是根据设备,屏幕大小和方向设计用户界面,而是鼓励开发人员根据活动大小类调整其应用程序的布局。我认为这是一个很好的举动。

With iOS 8 Apple introduced size classes for handling layouts of an app. Instead of designing user interfaces dependent on device, screen size, and orientation, developers are encouraged to rather adapt their app's layout depending on the active size classes. That's a good move, I think.

然而,当谈到iPad时,似乎没有办法区分不同的设备方向(提供截然不同的屏幕空间)并在我看来处理)。来自人机界面指南

如果我想提供侧边菜单,我现在应该做什么仅在横向方向,因为它提供更多空间,如 UISplitViewController

However, when it comes to the iPad there seem to be no way to distinguish the different device orientations (which provide quite different screen space and handling in my opinion). From the Human Interface Guidelines: What am I supposed to do now if I want to present a side menu only in landscape orientation because it provides more space, like a UISplitViewController?

UISplitViewController 另一种情况是Apple不吃它自己的狗粮,并使用其他指标,如设备方向或实际屏幕宽度来确定布局?或者还有另一种官方方式吗?在没有代码的情况下我可以在Interface Builder中做些什么?

Is the UISplitViewController another case where Apple doesn't eat it's own dog food and uses some other metrics like the device orientation or the actual screen width to determine the layout? Or is there another, official way to do that? Something I could do in Interface Builder alone without code?

(不,我不想使用 UISplitViewController 有几个原因。)

(And no, I don't want to use UISplitViewController for several reasons.)

推荐答案

这就是我解决你遇到的问题的方法:

This is how I resolved the issue you're having:

使用不同的大小类来添加纵向和横向的约束(您似乎正在做)然后为约束创建 IBOutletCollection 基于方向的每个尺寸类。

Use a different size class to add the constraints for portrait and landscape (which you seem to be doing) 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;
    }
}

这篇关于区分iPad方向与尺寸等级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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