簡體   English   中英

以編程方式使用自動布局的Scrollview

[英]Scrollview with autolayout programmatically

我想在視圖中添加2個UIScrollView 我添加了一個scrollview,它工作得很好。 然后我添加了其他scrollview。 但是現在它在第一個scrollview中崩潰了..

func setupTopModelScrollView()
{

    var viewBindingsDictBoth = [String: AnyObject]()
    viewBindingsDictBoth["shortListedScrollView"] = shortListedScrollView
    viewBindingsDictBoth["scrollViewTopModels"] = scrollViewTopModels
    viewBindingsDictBoth["contentView"] = contentView
    viewBindingsDictBoth["contentViewShortListed"] = contentViewShortListed
    viewBindingsDictBoth["lblTitle"] = lblTitle
    viewBindingsDictBoth["mainView"] = self.view

    scrollViewTopModels = UIScrollView(frame:CGRectZero)

    scrollViewTopModels.sizeToFit()

    view.addSubview(scrollViewTopModels)

    scrollViewTopModels.backgroundColor = UIColor.blueColor()

    contentView = UIView()
    contentView.backgroundColor = UIColor.redColor()
    scrollViewTopModels.addSubview(contentView)

    view.translatesAutoresizingMaskIntoConstraints = false
    scrollViewTopModels.translatesAutoresizingMaskIntoConstraints = false
    contentView.translatesAutoresizingMaskIntoConstraints = false

    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollViewTopModels]-0-|",options: [], metrics: nil,  views:viewBindingsDictBoth))

    let contentViewWidth : Int = arrTopModels.count * Int(SCREENWIDTH)

    scrollViewTopModels.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[contentView]-0-|",options: [], metrics: nil,  views:viewBindingsDictBoth))
    scrollViewTopModels.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[contentView]-0-|",options: [], metrics: nil,  views:viewBindingsDictBoth))

    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView(\(contentViewWidth))]|",options: [], metrics: nil, views:viewBindingsDictBoth))
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(==scrollViewTopModels)]",options: [], metrics: nil, views:viewBindingsDictBoth))

    scrollViewTopModels.showsHorizontalScrollIndicator = false
    scrollViewTopModels.bounces = false
    scrollViewTopModels.pagingEnabled = true


    for i in 0..<arrTopModels.count
    {

        let topModelView = TopModelCell(frame:CGRectZero)
        contentView.addSubview(topModelView)

        let spaceFromLeft : Int = i * Int(SCREENWIDTH)

        topModelView.translatesAutoresizingMaskIntoConstraints = false
        topModelView.imgModel.image = UIImage(named: arrTopModels.objectAtIndex(i) as! String)

        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-\(spaceFromLeft)-[topModelView(\(SCREENWIDTH))]",options: [], metrics: nil, views:viewBindingsDictBoth))

        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[topModelView(==contentView)]-0-|",options: [], metrics: nil,  views:viewBindingsDictBoth))

        print(topModelView)

        contentView.contentMode = UIViewContentMode.Redraw

    }

}

我收到此錯誤並崩潰..

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview 
H:|-0-[scrollViewTopModels]-0-| 
                           ^'

我被困了..如果有人可以提供幫助。

提前致謝..

我找到了答案。 我們需要在添加子視圖后聲明字典。 否則它將需要nill。

var viewBindingsDictBoth = [String: AnyObject]()
viewBindingsDictBoth["shortListedScrollView"] = shortListedScrollView
viewBindingsDictBoth["scrollViewTopModels"] = scrollViewTopModels
viewBindingsDictBoth["contentView"] = contentView
viewBindingsDictBoth["contentViewShortListed"] = contentViewShortListed
viewBindingsDictBoth["lblTitle"] = lblTitle
viewBindingsDictBoth["mainView"] = self.view

我們必須在分配所有視圖后編寫此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM