gpt4 book ai didi

ios - 以编程方式使用自动布局的 ScrollView

转载 作者:行者123 更新时间:2023-11-28 08:36:20 24 4
gpt4 key购买 nike

我正在尝试在 View 中添加 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-|
^'

我卡住了..如果有人能帮忙的话。

提前致谢..

最佳答案

我找到了答案。我们需要在添加 subview 后声明字典。否则它将花费 nill。

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

我们必须在分配所有 View 后编写此代码..

关于ios - 以编程方式使用自动布局的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37590847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com