属性 preservesSuperviewLayoutMargins 在 UIStackView 中被忽略
Property preservesSuperviewLayoutMargins is ignored in UIStackView
我正在尝试在尊重超级视图布局边距的滚动视图中创建堆栈视图。所以我将 preservesSuperviewLayoutMargins
设置为 true,并得出以下代码:
import UIKit
class ViewController: UIViewController {
let stackView: UIStackView = {
let sv = UIStackView()
sv.axis = .vertical
sv.preservesSuperviewLayoutMargins = true
sv.translatesAutoresizingMaskIntoConstraints = false
return sv
}()
let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.translatesAutoresizingMaskIntoConstraints = false
sv.alwaysBounceVertical = true
sv.preservesSuperviewLayoutMargins = true
return sv
}()
override func viewDidLoad() {
view.layoutMargins = UIEdgeInsets(top: 0, left: 64, bottom: 0, right: 64)
view.addSubview(scrollView)
scrollView.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.leftAnchor.constraint(equalTo: view.leftAnchor),
stackView.rightAnchor.constraint(equalTo: view.rightAnchor),
])
NSLayoutConstraint.activate([
scrollView.contentLayoutGuide.topAnchor.constraint(equalTo: stackView.topAnchor),
scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: stackView.bottomAnchor),
scrollView.contentLayoutGuide.leftAnchor.constraint(equalTo: stackView.leftAnchor),
scrollView.contentLayoutGuide.rightAnchor.constraint(equalTo: stackView.rightAnchor),
])
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: self.scrollView.topAnchor),
view.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
view.leftAnchor.constraint(equalTo: self.scrollView.leftAnchor),
view.rightAnchor.constraint(equalTo: self.scrollView.rightAnchor),
])
let l = UILabel()
l.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Itaque his sapiens semper vacabit. An est aliquid, quod te sua sponte delectet? Aliud igitur esse censet gaudere, aliud non dolere. Quasi ego id curem, quid ille aiat aut neget. Cur post Tarentum ad Archytam? Duo Reges: constructio interrete. Atque etiam valítudinem, vires, vacuitatem doloris non propter utilitatem solum, sed etiam ipsas propter se expetemus. Naturales divitias dixit parabiles esse, quod parvo esset natura contenta."
l.numberOfLines = 0
l.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(l)
super.viewDidLoad()
}
}
然而,lorem ipsum 文本完全忽略了左侧 64 和右侧 64 的边距。
我在这里错过了什么?
默认情况下,堆栈视图的排列的子视图通过堆栈视图的边缘...而不是边距受到限制。
您可以通过将堆栈视图的 .isLayoutMarginsRelativeArrangement
属性 设置为 true
:
来解决这个问题
let stackView: UIStackView = {
let sv = UIStackView()
sv.axis = .vertical
sv.preservesSuperviewLayoutMargins = true
sv.translatesAutoresizingMaskIntoConstraints = false
// add this line
sv.isLayoutMarginsRelativeArrangement = true
return sv
}()
我正在尝试在尊重超级视图布局边距的滚动视图中创建堆栈视图。所以我将 preservesSuperviewLayoutMargins
设置为 true,并得出以下代码:
import UIKit
class ViewController: UIViewController {
let stackView: UIStackView = {
let sv = UIStackView()
sv.axis = .vertical
sv.preservesSuperviewLayoutMargins = true
sv.translatesAutoresizingMaskIntoConstraints = false
return sv
}()
let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.translatesAutoresizingMaskIntoConstraints = false
sv.alwaysBounceVertical = true
sv.preservesSuperviewLayoutMargins = true
return sv
}()
override func viewDidLoad() {
view.layoutMargins = UIEdgeInsets(top: 0, left: 64, bottom: 0, right: 64)
view.addSubview(scrollView)
scrollView.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.leftAnchor.constraint(equalTo: view.leftAnchor),
stackView.rightAnchor.constraint(equalTo: view.rightAnchor),
])
NSLayoutConstraint.activate([
scrollView.contentLayoutGuide.topAnchor.constraint(equalTo: stackView.topAnchor),
scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: stackView.bottomAnchor),
scrollView.contentLayoutGuide.leftAnchor.constraint(equalTo: stackView.leftAnchor),
scrollView.contentLayoutGuide.rightAnchor.constraint(equalTo: stackView.rightAnchor),
])
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: self.scrollView.topAnchor),
view.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
view.leftAnchor.constraint(equalTo: self.scrollView.leftAnchor),
view.rightAnchor.constraint(equalTo: self.scrollView.rightAnchor),
])
let l = UILabel()
l.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Itaque his sapiens semper vacabit. An est aliquid, quod te sua sponte delectet? Aliud igitur esse censet gaudere, aliud non dolere. Quasi ego id curem, quid ille aiat aut neget. Cur post Tarentum ad Archytam? Duo Reges: constructio interrete. Atque etiam valítudinem, vires, vacuitatem doloris non propter utilitatem solum, sed etiam ipsas propter se expetemus. Naturales divitias dixit parabiles esse, quod parvo esset natura contenta."
l.numberOfLines = 0
l.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(l)
super.viewDidLoad()
}
}
然而,lorem ipsum 文本完全忽略了左侧 64 和右侧 64 的边距。
我在这里错过了什么?
默认情况下,堆栈视图的排列的子视图通过堆栈视图的边缘...而不是边距受到限制。
您可以通过将堆栈视图的 .isLayoutMarginsRelativeArrangement
属性 设置为 true
:
let stackView: UIStackView = {
let sv = UIStackView()
sv.axis = .vertical
sv.preservesSuperviewLayoutMargins = true
sv.translatesAutoresizingMaskIntoConstraints = false
// add this line
sv.isLayoutMarginsRelativeArrangement = true
return sv
}()