简体   繁体   中英

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. I just don't see the UIView in the simulator. I do have an IBOutlet to the UIScrollview...so I'm not sure why I'm running into this issue.

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. If I remove the UILable then I see the blue 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. 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. So, I added container UIView in the Storyboard first. ContainerView has equal width and height constraints to scrollView and I also added four constraints to get the containerView constrained to the 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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM