使用滚动视图错误以编程方式自动布局

时间:2016-06-24 22:27:57

标签: ios xcode swift uiscrollview autolayout

我试图在ViewController内部的paging-scrollView中添加一个视图,并且我遇到了很多自动布局问题(所有这些代码都是编程的,非故事板)。

第一组约束正常工作,但第二组(svfirstView)导致以下错误: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]))
}

1 个答案:

答案 0 :(得分:1)

正如@dan指出的那样,视觉格式中的字符串中存在拼写错误。如果有人遇到类似的问题,我会留下这篇文章。外卖:

带有自动布局的

terminating with uncaught exception of type NSException可能是由可视字符串中的拼写错误引起的。