如何解决第二次使用函数后,StackView出现问题
**嘿, 我有一个可以将我带到下一个VC的函数,但是一切都很好,但是当我第二次使用功能Button时,下一个VC只是一个白色视图...并且出现在控制台中:
将尝试通过打破约束来恢复
在UIViewAlertForUnsatisfiableConstraints处创建符号断点 在调试器中捕获它。中的方法 UIView中列出的UIConstraintBasedLayoutDebugging类别,在
也可能会有所帮助。 2020-11-09 05:44:46.315025 + 0100 PushUps + [4013:295478] [LayoutConstraints]无法 同时满足约束条件。可能至少有一个 以下列表中的约束是您不想要的约束。尝试这个: (1)查看每个约束,并尝试找出不存在的约束 期望; (2)找到添加了不必要约束的代码或 约束并修复它。 ( “ ”, “ ”, “ ”, “ “, “ (活动)>“) 将尝试通过打破约束来恢复
在UIViewAlertForUnsatisfiableConstraints处创建符号断点 在调试器中捕获它。中的方法 UIView中列出的UIConstraintBasedLayoutDebugging类别,在
可能也有帮助。
这是什么意思?当我除去所有高/宽约束之外的所有约束时,VC会完全移位,因此我需要所有x,y,顶部和底部锚点
StackView的代码:
class PushUpViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
activateProximitySensor()
startTimer()
view.backgroundColor = .white
setUpStackView()
}
func setUpStackView() {
// SetUp StackView:
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.alignment = .center
stackView.distribution = .fillEqually
stackView.spacing = 40
view.addSubview(stackView)
// SetUp StackView Constraints:
stackView.topAnchor.constraint(equalTo: view.topAnchor,constant: 30).isActive = true
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -50).isActive = true
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
// stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
// Set Elements to StackView:
stackView.addArrangedSubview(TimeLabel)
stackView.addArrangedSubview(PushUpButton)
// SetUp PushUpButton:
PushUpButton.backgroundColor = .white
PushUpButton.setTitle("\(count)",for: .normal)
PushUpButton.setTitleColor(.systemGray,for: .normal)
PushUpButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 70)
PushUpButton.translatesAutoresizingMaskIntoConstraints = false
PushUpButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
PushUpButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
PushUpButton.heightAnchor.constraint(equalToConstant: 500).isActive = true
PushUpButton.widthAnchor.constraint(equalToConstant: 400).isActive = true
// SetUp TimeLabel
TimeLabel.textAlignment = .center
TimeLabel.text = "\(counter)"
TimeLabel.textColor = .black
TimeLabel.font = .boldSystemFont(ofSize: 30)
self.view.addSubview(TimeLabel)
TimeLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
TimeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),TimeLabel.topAnchor.constraint(equalTo: view.topAnchor,constant: 10),TimeLabel.widthAnchor.constraint(equalToConstant: 200),TimeLabel.heightAnchor.constraint(equalToConstant: 200)
])
// SetUp SecondStackView
secondStackView.translatesAutoresizingMaskIntoConstraints = false
secondStackView.axis = .horizontal
secondStackView.alignment = .center
secondStackView.distribution = .fillEqually
view.addSubview(secondStackView)
// SetUp SecondStackView Constrains
secondStackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
secondStackView.topAnchor.constraint(equalTo: stackView.bottomAnchor,constant: 10).isActive = true
// Set Elements:
secondStackView.addArrangedSubview(breakButton)
secondStackView.addArrangedSubview(stopbutton)
//SetUp BreakButton
breakButton.backgroundColor = .lightGray
breakButton.setTitle("Break",for: .normal)
breakButton.setTitle("Start",for: .selected)
breakButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
breakButton.setTitleColor(.white,for: .normal)
breakButton.layer.cornerRadius = 12
breakButton.layer.borderWidth = 1
breakButton.layer.borderColor = UIColor.white.cgColor
breakButton.addTarget(self,action: #selector(BreakButtonTapped),for: .touchUpInside)
view.addSubview(breakButton)
breakButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
breakButton.centerXAnchor.constraint(equalTo: view.centerXAnchor,constant: -100),breakButton.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -30),breakButton.widthAnchor.constraint(equalToConstant: 150),breakButton.heightAnchor.constraint(equalToConstant: 50)
])
// SetUp StopButton:
stopbutton.backgroundColor = .systemRed
stopbutton.setTitle("Stop",for: .normal)
stopbutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
stopbutton.setTitleColor(.white,for: .normal)
stopbutton.layer.cornerRadius = 12
stopbutton.layer.borderWidth = 1
stopbutton.layer.borderColor = UIColor.white.cgColor
stopbutton.addTarget(self,action: #selector(stopButtonTapped),for: .touchUpInside)
view.addSubview(stopbutton)
stopbutton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stopbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor,constant: 100),stopbutton.bottomAnchor.constraint(equalTo: view.bottomAnchor,stopbutton.widthAnchor.constraint(equalToConstant: 150),stopbutton.heightAnchor.constraint(equalToConstant: 50)
])
}
解决方法
在将stackView添加为子视图之后,添加如下内容。好像丢失了。
stackView.translatesAutoresizingMaskIntoConstraints = false
,
好,您要为stackview
设置 top 和 bottom 约束以使其具有一定的高度,然后还要将高度约束设置为您的按钮。这将迫使 stackview 具有一定的大小,如果不可能的话,将导致您看到的错误。要从中恢复,操作系统将必须消除您冲突的约束之一,在您的情况下,这是按钮的高度。删除PushUpButton
上的高度限制,即可解决问题。让 stackview 进行工作并放置其排列的子视图。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。