ios - 尝试以编程方式在 ScrollView 中添加带有 SWIFT 约束的 UIView 失败

标签 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()

最佳答案

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

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 中布局多个 subview 时,在 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])

关于ios - 尝试以编程方式在 ScrollView 中添加带有 SWIFT 约束的 UIView 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39736614/

相关文章:

ios - 防止 sizeForItemAt 由于重用动态 collectionView 单元格而使用错误的单元格大小

ios - 我们如何使用 `Button` 添加 `TextField` 和 `SwiftUI`

ios - 如果将 UIView 添加为 subview ,则 UIButton 目标不起作用

ios - 使用信号量从 Parse 返回图像

IOS html 5 音频重复和跳过

swift - 计算 GPU 上的彩色像素 - 理论

iphone - 在包含一组 View 的 TableView 中添加 NSMutableArray

iphone - 在 UIView 中绘制线条动画

ios - Swift - fatal error : unexpectedly found nil while unwrapping an Optional values

objective-c - 内部使用 objective-c 代码的 Swift 2.1 框架(但不重新导出 objective-c