【问题标题】:How can I make a screen which has 2 views covering the screen and not overlapping如何制作一个有 2 个视图覆盖屏幕且不重叠的屏幕
【发布时间】:2016-05-03 12:22:17
【问题描述】:

我正在尝试创建一个带有 2 个视图的视图控制器,看起来就像在 IB 中一样

但它们只有在 iPhone 5s 上才有这种行为 在其他情况下,它看起来像横向图像上显示的方式。我使用尺寸类来设置不同屏幕方向的约束。

这就是它在 iPhone 6s plus 上的样子

【问题讨论】:

    标签: ios interface-builder nslayoutconstraint size-classes


    【解决方案1】:

    Any/Compact 是指任何具有 Compact 宽度的手机,小于 6+ 的 iPhone 就是这样。您需要设置 Regular/Compact 的约束,才能在 Portrait 中为 6+ 以外的 iPhone 设置约束。

    【讨论】:

    • 我这样做了,但没有帮助((
    • 如果您删除任何尺寸类的约束,其中任何宽度或高度都为 Any?只有正确定义的那些?
    【解决方案2】:

    使用 UIStackView,这种事情要容易得多

    1. 选择您的两个视图。
    2. 点击右下角的图标在它们周围创建一个 UIStackView。
    3. 将新的 UIStackView 按方向设置为水平或垂直。
    4. 将其固定到封闭视图。大功告成!

    有一个good tutorial here;我强烈建议您先练习一下使用堆栈视图进行设置,然后再以艰难的方式进行操作。

    【讨论】:

    • 谢谢。我正在考虑这一点,但我担心仅适用于 iOS 9.0 的事实。 15% 的设备在早期版本上运行
    • 听起来不错;但是这 15% 中有多少人会购买你的产品?对于大多数人来说,支持当前操作系统以外的任何东西都不值得额外的开发/测试/支持时间。尤其是在 OS 10 或 iOS X 或其他任何名称的周期中的这个阶段,将在接下来的几周内发布......
    • 谢谢你)我会按照你的建议做)
    【解决方案3】:

    你应该给出类似的约束,

    top, bottom, leading and trailing 到两个视图。

    然后选择两个视图并给equal height约束,你会得到想要的结果。

    您可以在any any 中提供。它适用于每个方向。

    希望这会有所帮助:)

    【讨论】:

    • 我需要这些视图在纵向和 iPad 上的特殊设计中相互叠加,因此任何任何一个都无济于事((我确实使用了这样的约束,但它们不起作用((() >
    • 不强制保留任何。我认为这种限制适用于每个尺寸等级。试试看。
    • 问题是它们根本不起作用((如果你愿意,我可以把源代码发给你
    【解决方案4】:

    将此添加到您的 viewController 的 viewDidLoad 方法中

    UIView *firstView = [[UIView alloc] init];     
    self.navigationController.navigationBar.translucent = false;
    firstView.backgroundColor = [UIColor redColor];
    
    firstView.translatesAutoresizingMaskIntoConstraints = false;
    [self.view addSubview:firstView];
        NSLayoutConstraint *topFirstViewYConstraint = [
    

    NSLayoutConstraint constraintWithItem:firstView 属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeTop 乘数:1.0f 常数:0.0f

    ];

    NSLayoutConstraint *centerFirstViewHeightConstraint = [ NSLayoutConstraint constraintWithItem:firstView 属性:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeHeight 乘数:0.5f 常数:0.0 ];

    NSLayoutConstraint *centerFirstViewWidthConstraint = [ NSLayoutConstraint constraintWithItem:firstView 属性:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeWidth 乘数:1.0f 常量:0.0f ]; [super.view addConstraints:@[ topFirstViewYConstraint, centerFirstViewHeightConstraint,centerFirstViewWidthConstraint]];

    UIView *secondView = [[UIView alloc] init];
    secondView.backgroundColor = [UIColor greenColor];
    secondView.translatesAutoresizingMaskIntoConstraints = false;
    [self.view addSubview:secondView];
    NSLayoutConstraint *topSecondViewYConstraint = [
    

    NSLayoutConstraint constraintWithItem:secondView 属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:firstView 属性:NSLayoutAttributeBottom 乘数:1.0f 常数:0.0f ]; NSLayoutConstraint *centerSecondViewHeightConstraint = [ NSLayoutConstraint constraintWithItem:secondView 属性:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeHeight 乘数:0.5f 常数:0.0 ]; NSLayoutConstraint *centerSecondViewWidthConstraint = [ NSLayoutConstraint constraintWithItem:secondView 属性:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeWidth 乘数:1.0f 常数:0.0f ]; [super.view addConstraints:@[ topSecondViewYConstraint, centerSecondViewHeightConstraint, centerSecondViewWidthConstraint]];

    【讨论】:

      【解决方案5】:

      所以问题似乎是 iPhone 6+ 之前的 iPhone 具有横向的 wCompact hCompact 尺寸类。你应该记住不要使用像 wCompact hAny 这样的大小类,因为它们会覆盖那个紧凑的大小类。

      所以,实际上,在这种情况下,您需要创建 3 个尺寸等级: wCompact hRegular - 所有肖像 wCompact hCompact - 6+ 之前的 iPhone wRegular hCompact - 所有其他

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-28
        • 2016-09-14
        相关资源
        最近更新 更多