[英]Scrollview with autolayout programmatically
I am trying to add 2 UIScrollView
in the view. 我想在视图中添加2个
UIScrollView
。 I have add one scrollview and it was working perfectly. 我添加了一个scrollview,它工作得很好。 Then I added other scrollview.
然后我添加了其他scrollview。 But now it is crashing in first 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
}
}
I am getting this error and crashing.. 我收到此错误并崩溃..
*** 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-|
^'
I am stuck.. If anyone can help. 我被困了..如果有人可以提供帮助。
Thanks in advance.. 提前致谢..
I have found the answer. 我找到了答案。 we need to declare the dictionary after adding the subview.
我们需要在添加子视图后声明字典。 otherwise it will take nill.
否则它将需要nill。
var viewBindingsDictBoth = [String: AnyObject]()
viewBindingsDictBoth["shortListedScrollView"] = shortListedScrollView
viewBindingsDictBoth["scrollViewTopModels"] = scrollViewTopModels
viewBindingsDictBoth["contentView"] = contentView
viewBindingsDictBoth["contentViewShortListed"] = contentViewShortListed
viewBindingsDictBoth["lblTitle"] = lblTitle
viewBindingsDictBoth["mainView"] = self.view
We have to write this code after allocating the all views.. 我们必须在分配所有视图后编写此代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.