以编程方式具有自动布局的滚动视图
Posted
技术标签:
【中文标题】以编程方式具有自动布局的滚动视图【英文标题】:Scrollview with autolayout programmatically 【发布时间】:2016-06-02 11:41:17 【问题描述】:我正在尝试在视图中添加 2 UIScrollView
。我添加了一个滚动视图,它运行良好。然后我添加了其他滚动视图。但是现在它在第一个滚动视图中崩溃了..
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-|
^'
我被困住了..如果有人可以提供帮助。
提前谢谢..
【问题讨论】:
错误听起来像您的“视图”变量尚未添加到任何超级视图。在添加约束之前,视图必须有父视图。 在关注@john_ryan 评论后,您应该关注以下内容:> 您的“view”变量不在 viewBindingsDictBoth 中。所以你也必须有 viewBindingsDictBoth["view"] = view。 【参考方案1】:我找到了答案。我们需要在添加子视图后声明字典。否则它将为零。
var viewBindingsDictBoth = [String: AnyObject]()
viewBindingsDictBoth["shortListedScrollView"] = shortListedScrollView
viewBindingsDictBoth["scrollViewTopModels"] = scrollViewTopModels
viewBindingsDictBoth["contentView"] = contentView
viewBindingsDictBoth["contentViewShortListed"] = contentViewShortListed
viewBindingsDictBoth["lblTitle"] = lblTitle
viewBindingsDictBoth["mainView"] = self.view
我们必须在分配所有视图后编写这段代码..
【讨论】:
以上是关于以编程方式具有自动布局的滚动视图的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式布局 UIScrollView,并为其子视图添加了自动布局,但它不滚动
UIScrollView 不使用自动布局滚动(内容视图框架大小以编程方式更改)