我有一个xib包含stackview叫newContainerView...当我尝试向它添加自定义视图时stackview使用addArrangedSubview它们相互堆积在一起,而不是像预期的那样垂直堆叠。因此,我用完全相同的代码创建了一个简单的测试项目,它运行得非常完美。我增加了高度和宽度约束,更改了translatesAutoresizingMaskIntoConstraints并比较这两个项目之间的每个设置。十次。
唯一的区别是,在实际项目中,容器视图被添加到UIViews...在示例项目中,只显示视图。但我看不出这会有什么不同的行为。
有人能解释一下为什么这段代码不起作用吗?
enter image description here
func makePhotoSheet(quote: Quote) {
let newContainerView = ContainerView(frame: CGRect(x: 0, y: 0, width: 612, height: 792))
//In the sample project, this replaces the line above
let newContainerView = ContainerView(frame: view.bounds)
view.addSubview(newContainerView)
//End sample project code
let testView1 = TestView()
let testView2 = TestView()
testView1.photo.image = UIImage(named: "bird")
testView1.photoCaption.text = "Bird"
testView1.heightAnchor.constraint(equalToConstant: 200).isActive = true
testView1.widthAnchor.constraint(equalToConstant: 1).isActive = true
testView1.translatesAutoresizingMaskIntoConstraints = false
testView2.photo.image = UIImage(named: "heart")
testView2.photoCaption.text = "Heart"
testView2.heightAnchor.constraint(equalToConstant: 200).isActive = true
testView2.widthAnchor.constraint(equalToConstant: 1).isActive = true
testView2.translatesAutoresizingMaskIntoConstraints = false
containerView.photoViewStackView.addArrangedSubview(testView1)
containerView.photoViewStackView.addArrangedSubview(testView2)
pdfPages.append(containerView)
}
这是自定义视图的XIB类,后面跟着XIB
class PhotoView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var photo: UIImageView!
@IBOutlet weak var photoCaption: UITextView!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
Bundle.main.loadNibNamed("PhotoView", owner: self, options: nil)
contentView.frame = self.bounds
addSubview(contentView)
}
}
这是容器视图的XIB类。
class PhotoSheet: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var photoViewStackView: UIStackView!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
Bundle.main.loadNibNamed("PhotoSheet", owner: self, options: nil)
contentView.frame = self.bounds
addSubview(contentView)
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。