简体   繁体   中英

Auto Layout Programmatically with Scroll View Error

I am trying to have add a view inside a paging-scrollView inside of a ViewController, and I am having a lot of Auto Layout problems (all of this code is programatic, non-Storyboard).

The first set of constraints work properly, but the second set ( sv and firstView ) cause the following error: libc++abi.dylib: terminating with uncaught exception of type NSException

Any help on this issue would be greatly appreciated, code below:

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]))
}

As @dan pointed out, there is a typo in the string in the visual format. I'll leave this post up in case anyone happens to run into a similar issue. The take-away:

terminating with uncaught exception of type NSException with Auto Layout might be caused by typos in the visual strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM