简体   繁体   English

区分iPad方向与尺寸等级

[英]Distinguish iPad orientation with size classes

With iOS 8 Apple introduced size classes for handling layouts of an app. iOS 8 Apple引入了用于处理应用程序布局的大小类 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. 我认为这是一个很好的举动。

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). 然而,当涉及到iPad时,似乎没有办法区分不同的设备方向(在我看来,它提供了截然不同的屏幕空间和处理)。 From the Human Interface Guidelines : 人机界面指南 iPad尺寸等级 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 ,我现在该UISplitViewController办?

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? UISplitViewController另一种情况,Apple不会吃它自己的狗食,并使用其他指标,如设备方向或实际屏幕宽度来确定布局? Or is there another, official way to do that? 或者还有另一种官方方式吗? Something I could do in Interface Builder alone without code? 在没有代码的情况下,我可以在Interface Builder中做些什么?

(And no, I don't want to use UISplitViewController for several reasons.) (不,我不想使用UISplitViewController有几个原因。)

This is how I resolved the issue you're having: 这就是我解决你遇到的问题的方法:

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. 使用不同大小的类来添加纵向和横向约束(您似乎正在执行此操作),然后为基于方向的每个尺寸类的约束创建IBOutletCollection

For example, I used wAnyhRegular to setup my portrait iPad layout and then used wRegularhAny to setup my landscape iPad layout. 例如,我使用wAnyhRegular设置我的纵向iPad布局,然后使用wRegularhAny设置我的横向iPad布局。 (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: (虽然您可能希望使用wRegular / hRegular作为您的方向布局之一,因为当您检查UITraitCollection时,iPad会注册为wRegular / UITraitCollection 。希望下面的代码演示了我如何去做:

@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. 我的风景也有3个限制。 肖像约束

I then apply the constraints as noted below (not shown, viewDidLoad executes _needsiPadConstraintsApplied = YES; ): 然后我应用如下所述的约束(未显示,viewDidLoad执行_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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM