我试图在ViewController的pageing-scrollView内添加一个视图,并且遇到很多自动布局问题(所有这些代码都是程序化的,非Storyboard的)。
第一组约束正常运行,但是第二组约束(sv
和firstView
)导致以下错误:libc++abi.dylib: terminating with uncaught exception of type NSException
对此代码的任何帮助将不胜感激,代码如下:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .whiteColor()
sv.backgroundColor = .redColor()
view.addSubview(sv)
sv.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[sv]|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["sv": sv]))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[sv]|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["sv": sv]))
sv.pagingEnabled = true
let firstView = UIView()
firstView.backgroundColor = .greenColor()
sv.addSubview(firstView)
firstView.translatesAutoresizingMaskIntoConstraints = false
sv.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[f]|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["fv": firstView]))
sv.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[fv]|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["fv": firstView]))
}
最佳答案
正如@dan指出的那样,字符串在视觉格式中有一个错字。如果有人碰巧遇到类似问题,我将保留此帖子。外卖:
具有自动布局的terminating with uncaught exception of type NSException
可能是由于视觉字符串中的错字引起的。
关于ios - 通过编程自动布局,但出现 ScrollView 错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38022966/