[英]Xcode Split the Screen into 4 Giant Buttons
I have figured out how to use auto layout to make all buttons visible, but I am stuck in one particular goal: 我想出了如何使用自动布局使所有按钮可见的方法,但是我陷入了一个特定的目标:
I am trying to divide the screen into 4 giant buttons, each button occupies 1/2 of the screen height and 1/2 of the screen width, with no spacing between each other. 我试图将屏幕分成4个巨型按钮,每个按钮占据屏幕高度的1/2和屏幕宽度的1/2,彼此之间没有间距。 I tried setting constraints, but the buttons would not display properly.
我尝试设置约束,但是按钮无法正确显示。
Please see the two images I posted here, I want to make it so that the iPhone SE view looks the same as the iPhone 7 view. 请查看我在此处发布的两个图像,以使其成为iPhone SE视图与iPhone 7视图相同。 Last time I posted similar question my reputation got decreased, although I did not know why, I am someone who has just started on my own and am trying to figure things out so please advice.
上次我发布类似的问题时,我的声誉下降了,尽管我不知道为什么,但我是一个刚起步并试图弄清问题的人,请提出建议。 Thank you!
谢谢!
Following is the screenshot of constraints for your desired layout. 以下是您所需布局的约束的屏幕截图。
I'm gonna attach the storyboard also. 我还要附上情节提要。 File Download Link
文件下载链接
Without Top and Bottom Views 没有顶视图和底视图
This can be done with constraints but it can also be easily done with stackViews. 这可以通过约束来完成,但是也可以使用stackViews轻松完成。
let button1 = UIButton()
button1.backgroundColor = .red
let button2 = UIButton()
button2.backgroundColor = .blue
let button3 = UIButton()
button3.backgroundColor = .green
let button4 = UIButton()
button4.backgroundColor = .orange
let topStack = UIStackView(arrangedSubviews: [button1, button2])
topStack.axis = .horizontal
topStack.distribution = .fillEqually
let bottomStack = UIStackView(arrangedSubviews: [button3, button4])
bottomStack.axis = .horizontal
bottomStack.distribution = .fillEqually
let stackView = UIStackView(arrangedSubviews: [topStack, bottomStack])
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.frame = view.bounds
stackView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
view.addSubview(stackView)
Follow these steps: ( Very simple easy to implement ) 请按照以下步骤操作:( 非常简单容易实现 )
Top, Bottom, Leading & Trailing constraint
with constant value = 0 (with respect to position you have shown in your snapshot) Top, Bottom, Leading & Trailing constraint
。 Equal Width Constraint
Equal Width Constraint
Equal Width Constraint
Equal Width Constraint
Equal Height Constraint
Equal Height Constraint
Equal Height Constraint
Equal Height Constraint
Look at this snapshot (Reference constraints, for all button are mentioned in left pane) 查看此快照(在左窗格中提到了所有按钮的参考约束)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.