将按钮中间放在手机屏幕的底部(无论屏幕大小如何)

时间:2016-07-18 17:42:04

标签: ios objective-c constraints

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

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

enter image description here

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

enter image description here

我试过这个:

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

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

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

请帮助,我已经尝试了几天,到处搜索,但找不到一个线索。谢谢!

3 个答案:

答案 0 :(得分:1)

[qrScanner.centerYAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor constant:0].active = YES;

答案 1 :(得分:0)

基于你的模型的样子,我猜你的按钮没有固定的大小,而是根据容器的大小增长/缩小。因此,只有当您的按钮的高度为200时,偏移量100才有效。

我不确定你是如何确定按钮的大小的,但是就定位而言,你想要将其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];

答案 2 :(得分:0)

Swift 4.2

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