我使用可视语言制作约束,但每当我尝试添加它们时都会导致错误。
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)
}
}
答案 0 :(得分:2)
Xcode为您提供错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format:
Expected a view
V:|==[timer]==|
^'
所以删除==它会起作用。