[英]Swift: Autolayout programmatically bottom anchor is not working correctly
I have set constraints for my button in the code and this is what my code looks like:我在代码中为我的按钮设置了约束,这就是我的代码的样子:
addSubview(stackViewOne)
stackViewOne.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -250).isActive = true
stackViewOne.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
stackViewOne.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
stackViewOne.heightAnchor.constraint(equalToConstant: frame.size.height * 0.065).isActive = true
but now i have problems with my bottom anchor.但现在我的底锚有问题。 I want to make my stackview 80 from the bottom away.
我想从底部使我的stackview 80。 Only when I enter eighty as the distance my stackview is not displayed.
只有当我输入八十作为距离时,我的堆栈视图才会显示。 Only when I enter a really high value in the minus range is the stackview only displayed.
只有当我在负范围内输入一个非常高的值时,才会显示堆栈视图。 (equalTo: bottomAnchor, constant: -250)
(等于:bottomAnchor,常量:-250)
Does anyone know why?有谁知道为什么? Best regards
此致
You usually don't need to specify a height constraint on a stackview - the stackview itself will be sized by its arrangedsubviews (labels?).您通常不需要在 stackview 上指定高度约束 - stackview 本身将通过其排列的子视图(标签?)来调整大小。 I'd start by removing that constraint.
我将首先删除该约束。 Make sure your arranged subviews have sizes and your stackview's spacing/alignment etc is configured correctly.
确保您排列的子视图具有大小,并且您的堆栈视图的间距/对齐等配置正确。
Also possible safeArea / layoutMargins are interfering with your layout - but impossible to know without more of your code.也可能 safeArea / layoutMargins 干扰您的布局 - 但如果没有更多代码就不可能知道。
I created a UIView class and there I created objects look: import UIKit `` class TimerSequenzeView: UIView { I created a UIView class and there I created objects look: import UIKit `` class TimerSequenzeView: UIView {
lazy var plusButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("5", for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 35)
btn.titleLabel?.font = UIFont(name: "Arial", size: 35)
btn.setTitleColor(.red, for: .normal)
btn.layer.borderColor = UIColor.red.cgColor
btn.layer.borderWidth = 3
btn.layer.cornerRadius = 30
btn.setImage(UIImage(systemName: "plus"), for: .normal)
btn.tintColor = .red
btn.translatesAutoresizingMaskIntoConstraints = false
return btn
}()
lazy var minusButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("5", for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 35)
btn.titleLabel?.font = UIFont(name: "Arial", size: 35)
btn.setTitleColor(.red, for: .normal)
btn.layer.borderColor = UIColor.red.cgColor
btn.layer.borderWidth = 3
btn.layer.cornerRadius = 30
btn.setImage(UIImage(systemName: "minus"), for: .normal)
btn.tintColor = .red
btn.translatesAutoresizingMaskIntoConstraints = false
return btn
}()
lazy var stackViewOne: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.spacing = 20
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(plusButton)
stackView.addArrangedSubview(minusButton)
return stackView
}()
override init(frame: CGRect) {
super.init(frame: frame)
setConstrains()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func setConstrains() {
addSubview(stackViewOne)
stackViewOne.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -80).isActive = true
stackViewOne.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
stackViewOne.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
stackViewOne.heightAnchor.constraint(equalToConstant: frame.size.height * 0.065).isActive = true
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.