简体   繁体   English

Xcode将屏幕分成4个巨型按钮

[英]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! 谢谢!

iPhone7查看

iPhoneSE查看

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 没有顶视图和底视图

在此处输入图片说明

File Download Link 文件下载链接

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 ) 请按照以下步骤操作:( 非常简单容易实现

  1. Remove all constrains from all your views (Orange, Red, Blue and Green) 删除所有视图(橙色,红色,蓝色和绿色)中的所有约束
  2. Select All Four Views and assign Top, Bottom, Leading & Trailing constraint with constant value = 0 (with respect to position you have shown in your snapshot) 选择“所有四个视图”,并为常量,值(相对于快照中显示的位置)分配“ Top, Bottom, Leading & Trailing constraint
  3. Select Orange and Red views and assign Equal Width Constraint 选择橙色和红色视图并分配Equal Width Constraint
  4. Select Blue and Green views and assign Equal Width Constraint 选择“蓝色”和“绿色”视图并分配“ Equal Width Constraint
  5. Select Orange and Blue views and assign Equal Height Constraint 选择橙色和蓝色视图并分配Equal Height Constraint
  6. Select Red and Green views and assign Equal Height Constraint 选择红色和绿色视图并分配Equal Height Constraint

Look at this snapshot (Reference constraints, for all button are mentioned in left pane) 查看此快照(在左窗格中提到了所有按钮的参考约束)

在此处输入图片说明

Final View: 最终视图: 在此处输入图片说明

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

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