gpt4 book ai didi

ios - 为什么我创建的 stackView 不显示?

转载 作者:行者123 更新时间:2023-11-28 14:18:14 28 4
gpt4 key购买 nike

class EventsCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setUpCell()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

lazy var containerView: UIView = {
let container = UIView()
container.frame = eventImage.frame
container.addSubview(eventImage)
container.layer.masksToBounds = false
container.translatesAutoresizingMaskIntoConstraints = false
extensionSetConstraintsWithFormat(horizontalFormat: "|[v0]|", verticalFormat: "|[v0]|", views: [eventImage])
container.extensionSetShadowEasy(color: UIColor.black.cgColor), offSet: CGSize(width: 0, height: 3))
return container
}()

let eventImage: UIImageView = {
let eventView = UIImageView()
eventView.backgroundColor = UIColor.red
eventView.layer.cornerRadius = 10
eventView.translatesAutoresizingMaskIntoConstraints = false
eventView.layer.masksToBounds = true
eventView.image = UIImage(named: "Event")
return eventView
}()

let usersStackView: UIStackView = {
let stack = UIStackView()
stack.backgroundColor = UIColor.orange
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .horizontal
stack.distribution = .fillProportionally
stack.layer.cornerRadius = 10
stack.extensionSetShadowEasy(color: UIColor.black.cgColor), offSet: CGSize(width: 0, height: 3))
return stack
}()


func setUpCell(){
layer.masksToBounds = false
backgroundColor = UIColor.white
layer.cornerRadius = 10
extensionSetShadowEasy(color: UIColor.black.cgColor), offSet: CGSize(width: 5, height: 5))
addSubview(containerView)
addSubview(usersStackView)

extensionSetConstraintsWithFormat(horizontalFormat: "|-10-[v0]-10-|", verticalFormat: "|-10-[v0(200)][v1(50)]-10-|", views: [containerView, usersStackView])
extensionSetConstraintsWithFormat(horizontalFormat: "|-10-[v0]-10-|", verticalFormat: nil, views: [usersStackView])


}

}

我正在尝试将 UIStackView 添加到 collectionViewCell 但它根本没有显示,我不知道我的代码中缺少什么,我设置了约束但它似乎不起作用,可以添加其他 View 堆栈 View 没有出现...

我用来添加约束的函数是我创建的一个扩展,但我已经对其进行了多次测试并且它运行良好。

出于学习目的,我通过代码创建所有内容,没有使用 Storyboard,仅使用代码..

预先感谢您的回答)抱歉我的错误代码,我是初学者...

最佳答案

我的经验告诉我,你不能渲染 UIStackView,因为它是空的,如果 UIStackView 没有任何子元素,它就不会被渲染,所以设置背景颜色或任何其他属性都没有关系,因为它们不会显示。

如果即使 UIStackView 为空也想显示背景,可以将其替换为 UIView 并在其中放置 UIStackView,只需相应地添加约束和属性即可。希望对您有所帮助。

关于ios - 为什么我创建的 stackView 不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045300/

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