【问题标题】:Place middle of button at the bottom of phone screen (regardless of the screen size)将按钮中间放在手机屏幕底部(无论屏幕大小)
【发布时间】:2016-07-18 17:42:03
【问题描述】:

我想问如何设置一个约束,使按钮的中间位于屏幕底部以适应不同的 iOS 屏幕尺寸?

这是最理想的情况,其中按钮的中间部分位于屏幕底部,而下半部分不显示在屏幕上:

使用下面的代码,这就是正在发生的事情,这不是我想要的:

我试过这个:

[qrScanner.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor constant:100].active = YES;

但它只适用于 iPhone 6 屏幕,不适用于其他屏幕,例如 iPad Mini。

我想知道是否有任何方法可以推广这样的公式,以便无论屏幕大小如何,所有按钮的中心都很好地放置在屏幕底部?

请帮忙,我已经尝试了几天并到处搜索,但找不到任何线索。谢谢!

【问题讨论】:

标签: ios objective-c constraints


【解决方案1】:
[qrScanner.centerYAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor constant:0].active = YES;

【讨论】:

    【解决方案2】:

    根据您的模型外观,我猜您的按钮没有固定大小,而是根据容器的大小而增长/缩小。因此,100 的偏移量仅在您的按钮的高度恰好为 200 时才有效。

    我不确定您如何确定按钮的大小,但就定位而言,您希望将其 X 坐标居中,然后设置底部约束,使按钮的中心 Y val 等于容器的底部.以下是您需要为定位添加的 2 个约束:

    // Center X value in the view
    [NSLayoutConstraint constraintWithItem:button
                                 attribute:NSLayoutAttributeCenterX
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.view
                                 attribute:NSLayoutAttributeCenterX
                                multiplier:1
                                  constant:0];
    
    // Center button's Y value to the bottom of the view
    [NSLayoutConstraint constraintWithItem:button
                                 attribute:NSLayoutAttributeCenterY
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.view
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1
                                  constant:0];
    

    【讨论】:

      【解决方案3】:

      Swift 4.2

      qrScanner.centerYAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
      

      【讨论】:

      • centerYAnchor 和 bottomAnchor 的类型相同。两者都是 NSLayoutYAxisAnchor。所以你可以像这样使用它们。有关更多信息和完整示例,我在此链接stackoverflow.com/a/54878823/9614722 中回复类似问题。
      猜你喜欢
      • 2011-05-14
      • 1970-01-01
      • 2013-01-24
      • 2016-03-27
      • 2013-10-13
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多