使用自动布局将 space 个对象均匀分开
use auto layout to space objects evenly apart
我下面的代码试图基于下面的照片。 PicLocate 应占视图高度的 60%。 Pic[0] 和 Pic1 应该占据 10% 的高度,甚至 width.All 的对象应该在视图之间间隔 10 - 15 的间距。主要是应该和照片一样
import UIKit
class ViewController: UIViewController {
var picLocate = UIImageView()
var jessicaAlba:Float = 50
var topConstraint: NSLayoutConstraint!
var heightConstraint: NSLayoutConstraint!
var leadingConstraint: NSLayoutConstraint!
var trailingConstraint: NSLayoutConstraint!
let pic = (0..<3).map { _ in UIButton() }
override func viewDidLoad() {
super.viewDidLoad()
[picLocate,pic[0],pic[1]].forEach {
[=11=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=11=])
[=11=].backgroundColor = .systemOrange
}
pic[0].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10).isActive = true
pic[0].leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
pic[0].trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -300).isActive = true
pic[0].bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -300).isActive = true
pic[1].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10).isActive = true
pic[1].leadingAnchor.constraint(equalTo: pic[0].trailingAnchor, constant: 10).isActive = true
pic[1].trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
pic[1].bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -300).isActive = true
pic[0].setTitle("Add Box", for: .normal)
pic[1].setTitle("Save Photo", for: .normal)
topConstraint = picLocate.topAnchor.constraint(equalTo: view.topAnchor, constant: CGFloat(jessicaAlba))
topConstraint.isActive = true
heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: CGFloat(-jessicaAlba))
heightConstraint.isActive = true
leadingConstraint = picLocate.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(jessicaAlba))
leadingConstraint.isActive = true
trailingConstraint = picLocate.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-jessicaAlba))
trailingConstraint.isActive = true
}
}
在编写约束时对其进行注释可能会很有帮助。很多时候,这会让你意识到你想做什么...
要获得所需的布局,您需要:
- 将
picLocate
高度设置为视图高度的 60%(使用您当前的代码,您还从这 60% 中减去了 50 磅)
- 将左侧图片(按钮)的行距设置为
picLocate
的行距
- 将右侧图片(按钮)的尾部设置为
picLocate
的尾部
- 将右图前导设置为左图尾随 + 10 磅间距
- 然后 设置右图宽度等于左图宽度
结果:
并且在横向模式下,它会自动调整到所需的百分比:
这是您的代码,包含我描述的更改。看一看,评论评论:
class ViewController: UIViewController {
var picLocate = UIImageView()
var jessicaAlba:CGFloat = 50
var topConstraint: NSLayoutConstraint!
var heightConstraint: NSLayoutConstraint!
var leadingConstraint: NSLayoutConstraint!
var trailingConstraint: NSLayoutConstraint!
let pic = (0..<3).map { _ in UIButton() }
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
[picLocate,pic[0],pic[1]].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
[=10=].backgroundColor = .systemOrange
}
// 50-pts (jessicaAlba) from top of view
topConstraint = picLocate.topAnchor.constraint(equalTo: view.topAnchor, constant: jessicaAlba)
// 60% of view height minus 50-pts == ((0.6 * view height) - 50)
heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: -jessicaAlba)
// if you actually want 60% of view height, set constant to 0.0
//heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: 0.0)
// 50-pts leading and trailing
leadingConstraint = picLocate.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: jessicaAlba)
trailingConstraint = picLocate.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -jessicaAlba)
NSLayoutConstraint.activate([
topConstraint,
heightConstraint,
leadingConstraint,
trailingConstraint,
// "left pic" top is 10-pts below "picLocate" bottom
pic[0].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10.0),
// "left pic" leading equal to "picLocate" leading
pic[0].leadingAnchor.constraint(equalTo: picLocate.leadingAnchor, constant: 0.0),
// "left pic" height equal to 10% of view height
pic[0].heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10),
// "right pic" top equal to "left pic" top
pic[1].topAnchor.constraint(equalTo: pic[0].topAnchor),
// "right pic" height equal to "left pic" height
pic[1].heightAnchor.constraint(equalTo: pic[0].heightAnchor),
// "right pic" trailing equal to "picLocate" trailing
pic[1].trailingAnchor.constraint(equalTo: picLocate.trailingAnchor, constant: 0.0),
// "right pic" leading equal to "left pic" trailing plust 10-pts
pic[1].leadingAnchor.constraint(equalTo: pic[0].trailingAnchor, constant: 10.0),
// "right pic" width equal to "left pic" width
pic[1].widthAnchor.constraint(equalTo: pic[0].widthAnchor),
])
pic[0].setTitle("Add Box", for: .normal)
pic[1].setTitle("Save Photo", for: .normal)
}
}
我下面的代码试图基于下面的照片。 PicLocate 应占视图高度的 60%。 Pic[0] 和 Pic1 应该占据 10% 的高度,甚至 width.All 的对象应该在视图之间间隔 10 - 15 的间距。主要是应该和照片一样
import UIKit
class ViewController: UIViewController {
var picLocate = UIImageView()
var jessicaAlba:Float = 50
var topConstraint: NSLayoutConstraint!
var heightConstraint: NSLayoutConstraint!
var leadingConstraint: NSLayoutConstraint!
var trailingConstraint: NSLayoutConstraint!
let pic = (0..<3).map { _ in UIButton() }
override func viewDidLoad() {
super.viewDidLoad()
[picLocate,pic[0],pic[1]].forEach {
[=11=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=11=])
[=11=].backgroundColor = .systemOrange
}
pic[0].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10).isActive = true
pic[0].leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
pic[0].trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -300).isActive = true
pic[0].bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -300).isActive = true
pic[1].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10).isActive = true
pic[1].leadingAnchor.constraint(equalTo: pic[0].trailingAnchor, constant: 10).isActive = true
pic[1].trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
pic[1].bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -300).isActive = true
pic[0].setTitle("Add Box", for: .normal)
pic[1].setTitle("Save Photo", for: .normal)
topConstraint = picLocate.topAnchor.constraint(equalTo: view.topAnchor, constant: CGFloat(jessicaAlba))
topConstraint.isActive = true
heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: CGFloat(-jessicaAlba))
heightConstraint.isActive = true
leadingConstraint = picLocate.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(jessicaAlba))
leadingConstraint.isActive = true
trailingConstraint = picLocate.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-jessicaAlba))
trailingConstraint.isActive = true
}
}
在编写约束时对其进行注释可能会很有帮助。很多时候,这会让你意识到你想做什么...
要获得所需的布局,您需要:
- 将
picLocate
高度设置为视图高度的 60%(使用您当前的代码,您还从这 60% 中减去了 50 磅) - 将左侧图片(按钮)的行距设置为
picLocate
的行距
- 将右侧图片(按钮)的尾部设置为
picLocate
的尾部
- 将右图前导设置为左图尾随 + 10 磅间距
- 然后 设置右图宽度等于左图宽度
结果:
并且在横向模式下,它会自动调整到所需的百分比:
这是您的代码,包含我描述的更改。看一看,评论评论:
class ViewController: UIViewController {
var picLocate = UIImageView()
var jessicaAlba:CGFloat = 50
var topConstraint: NSLayoutConstraint!
var heightConstraint: NSLayoutConstraint!
var leadingConstraint: NSLayoutConstraint!
var trailingConstraint: NSLayoutConstraint!
let pic = (0..<3).map { _ in UIButton() }
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
[picLocate,pic[0],pic[1]].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
[=10=].backgroundColor = .systemOrange
}
// 50-pts (jessicaAlba) from top of view
topConstraint = picLocate.topAnchor.constraint(equalTo: view.topAnchor, constant: jessicaAlba)
// 60% of view height minus 50-pts == ((0.6 * view height) - 50)
heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: -jessicaAlba)
// if you actually want 60% of view height, set constant to 0.0
//heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: 0.0)
// 50-pts leading and trailing
leadingConstraint = picLocate.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: jessicaAlba)
trailingConstraint = picLocate.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -jessicaAlba)
NSLayoutConstraint.activate([
topConstraint,
heightConstraint,
leadingConstraint,
trailingConstraint,
// "left pic" top is 10-pts below "picLocate" bottom
pic[0].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10.0),
// "left pic" leading equal to "picLocate" leading
pic[0].leadingAnchor.constraint(equalTo: picLocate.leadingAnchor, constant: 0.0),
// "left pic" height equal to 10% of view height
pic[0].heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10),
// "right pic" top equal to "left pic" top
pic[1].topAnchor.constraint(equalTo: pic[0].topAnchor),
// "right pic" height equal to "left pic" height
pic[1].heightAnchor.constraint(equalTo: pic[0].heightAnchor),
// "right pic" trailing equal to "picLocate" trailing
pic[1].trailingAnchor.constraint(equalTo: picLocate.trailingAnchor, constant: 0.0),
// "right pic" leading equal to "left pic" trailing plust 10-pts
pic[1].leadingAnchor.constraint(equalTo: pic[0].trailingAnchor, constant: 10.0),
// "right pic" width equal to "left pic" width
pic[1].widthAnchor.constraint(equalTo: pic[0].widthAnchor),
])
pic[0].setTitle("Add Box", for: .normal)
pic[1].setTitle("Save Photo", for: .normal)
}
}