gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:02:50 24 4
gpt4 key购买 nike

所以我一直在尝试以编程方式将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com