我正在对视觉语言进行约束,但每当我尝试添加它们时都会导致错误。
class TimerViewController : UIViewController{
let timer = TimerView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.addSubview(timer)
self.createContraints()
}
func createContraints(){
self.timer.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraints =
NSLayoutConstraint.constraints(withVisualFormat: "H:|[timer]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["timer" : timer])
let verticleContraints =
NSLayoutConstraint.constraints(withVisualFormat: "V:|==[timer]==|", options: NSLayoutFormatOptions(), metrics: nil, views: ["timer" : timer])
self.view.addConstraints(horizontalConstraints)
self.view.addConstraints(verticleContraints)
}
}
最佳答案
Xcode 给你错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format:
Expected a view
V:|==[timer]==|
^'
所以去掉 == 就可以了。
关于iOS 约束导致 SIGABRT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127321/