尝试使用SWIFT中的约束以编程方式在滚动视图中添加UIView失败

时间:2016-09-28 01:24:20

标签: ios swift uiview uiscrollview constraints

所以我一直试图以编程方式将UIVIew添加到UIScrollView中。我只是没有在模拟器中看到UIView。 我确实有一个关于UIScrollview的IBOutlet ......所以我不确定为什么我会遇到这个问题。

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])

}

我删除了约束,仍然看不到UILabel或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()

1 个答案:

答案 0 :(得分:0)

我认为您需要在ownerView和importantScoll之间设置相等的宽度约束,因为在没有任何适当的宽度约束之前,您的滚动视图无法确定内容大小。您可以添加此行。

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])

更新:  在ScrollView中布置多个子视图时,在Scrollview中添加另一个容器UIView是很常见的。所以,我首先在Storyboard中添加了容器UIView。 ContainerView对scrollView具有相同的宽度和高度约束,我还添加了四个约束来使containerView受限于ScrollView。最后,我按照以下方式设置了编程约束。

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])