简体   繁体   English

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

[英]Place middle of button at the bottom of phone screen (regardless of the screen size)

I would like to ask how can one set a constraint such that the middle of a button is located at the bottom of the screen for different iOS screen size? 我想问一下如何针对不同的iOS屏幕尺寸设置约束,以使按钮的中间位置位于屏幕底部?

This is the ideal case where middle of button is placed at the bottom of the screen, and the bottom half is not shown on screen: 这是将按钮的中部放在屏幕底部,而下半部不显示在屏幕上的理想情况:

在此处输入图片说明

With the below code, this is what's happening, which is not what I wanted: 使用下面的代码,这就是发生的事情,这不是我想要的:

在此处输入图片说明

I have tried this: 我已经试过了:

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

but it only works on iPhone 6 screen, not on other screen, such as iPad Mini. 但它仅适用于iPhone 6屏幕,不适用于其他屏幕,例如iPad Mini。

May I know if there's any way that can generalise such formula so that all centre of buttons are nicely placed at the bottom of the screen regardless of screen size? 我能否知道是否有任何方法可以推广这种公式,以便无论屏幕大小如何,所有按钮的中心都很好地放置在屏幕底部?

Please help, I have tried for days and searched everywhere but cannot find a single clue. 请帮忙,我已经尝试了好几天,并且在各处搜索,但找不到任何线索。 Thanks! 谢谢!

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

Based off what your mockups look like, I'm guessing that your button does not have a fixed size, but rather it grows/shrinks depending on the size of the container. 根据您的模型外观,我猜测您的按钮没有固定的大小,而是根据容器的大小而增加/缩小。 Because of this, the offset of 100 only works when your button happens to have a height of 200. 因此,仅当按钮的高度为200时,偏移100才有效。

I'm not sure how you determine the size of the button, but as far as positioning goes, you want to center its X coordinate, and then set a bottom constraint where the button's center Y val is equal to the container's bottom. 我不确定如何确定按钮的大小,但是就定位而言,您要使其X坐标居中,然后设置一个底部约束,使按钮的中心Y val等于容器的底部。 These are the 2 constraints you will need to add for positioning: 这些是您需要添加的两个约束条件:

// 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];

斯威夫特4.2

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

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

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