[英]Failing at trying to add a UIView inside a scroll view programmatically with constraints in SWIFT
So I've been trying to add a UIVIew to a UIScrollView programatically with constraints. 所以我一直在尝试通过约束将UIVIew以编程方式添加到UIScrollView中。 I just don't see the UIView in the simulator.
我只是在模拟器中看不到UIView。 I do have an IBOutlet to the UIScrollview...so I'm not sure why I'm running into this issue.
我确实有一个IBOutlet到UIScrollview ...所以我不确定为什么会遇到这个问题。
override func viewDidAppear(animated: Bool) {
let ownerUIView:UIView! = UIView()
var innerLabel:UILabel = UILabel()
ownerUIView.translatesAutoresizingMaskIntoConstraints = false
innerLabel.translatesAutoresizingMaskIntoConstraints = false
innerLabel.text = "Test text"
ownerUIView.addSubview(innerLabel)
importantScroll.addSubview(ownerUIView)
ownerUIView.backgroundColor = UIColor.blueColor()
let constraint1 = NSLayoutConstraint(item: ownerUIView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: importantScroll, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 10)
let constraint2 = NSLayoutConstraint(item: ownerUIView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: importantScroll, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 10)
let constraint3 = NSLayoutConstraint(item: ownerUIView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 60)
let constraint4 = NSLayoutConstraint(item: ownerUIView, attribute: .Top , relatedBy: .Equal, toItem: importantScroll, attribute: NSLayoutAttribute.Top , multiplier: 1, constant: 20)
NSLayoutConstraint.activateConstraints([constraint1,constraint2,constraint3,constraint4])
}
I removed the constraints and still can't see the UILabel or the UIVIew. 我删除了约束,但仍然看不到UILabel或UIVIew。 If I remove the UILable then I see the blue UIView.
如果删除UILable,则会看到蓝色的UIView。
let ownerUIView:UIView = UIView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))
let innerLabel:UILabel = UILabel(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
ownerUIView.translatesAutoresizingMaskIntoConstraints = false
innerLabel.translatesAutoresizingMaskIntoConstraints = false
innerLabel.text = "Test text"
ownerUIView.addSubview(innerLabel)
importantScroll.addSubview(ownerUIView)
ownerUIView.backgroundColor = UIColor.blueColor()
I think you need to set equal width constraint between your ownerView and importantScoll because your scrollview can not determine content size until there isn't any proper width constraint. 我认为您需要在ownerView和ImportantScoll之间设置相等的宽度约束,因为在没有适当的宽度约束之前,scrollview才能确定内容大小。 You can add this line.
您可以添加此行。
let constraint5 = NSLayoutConstraint(item: ownerUIView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: importantScroll, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activateConstraints([constraint1,constraint2,constraint3,constraint4,constraint5])
Update: It is quite common that adding another container UIView inside of the Scrollview when laying out multiple subviews in the ScrollView. 更新:在ScrollView中布置多个子视图时,通常会在Scrollview中添加另一个容器UIView。 So, I added container UIView in the Storyboard first.
因此,我首先在情节提要中添加了容器UIView。 ContainerView has equal width and height constraints to scrollView and I also added four constraints to get the containerView constrained to the ScrollView.
ContainerView具有与scrollView相同的宽度和高度约束,我还添加了四个约束以将containerView约束到ScrollView。 Lastly, I set constraints programmaticallly as follows.
最后,我通过编程将约束设置如下。
let constraint6 = NSLayoutConstraint(item: ownerView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: containerView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 40)
let constraint7 = NSLayoutConstraint(item: containerView, attribute: .Tailing, relatedBy: .Equal, toItem: ownerView, attribute: .Trailing, multiplier: 1, constant: 10)
let constraint8 = NSLayoutConstraint(item: ownerView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 60)
let constraint9 = NSLayoutConstraint(item: ownerView, attribute: .Top , relatedBy: .Equal, toItem: containerView, attribute: NSLayoutAttribute.Top , multiplier: 1, constant: 20)
containerView.addConstraints([constraint6,constraint7,constraint8,constraint9])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.