How to set @QuickLayout view frame in UIViewController? #8
Answered
by
constantine-fry
zeroskylian
asked this question in
Q&A
|
How to automatically set MyCellView frame class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// how to set MyCellView frame
view.addSubview(MyCellView())
}
}@QuickLayout
class MyCellView: UIView {
let titleLabel = UILabel(text: "titleLabel")
let subtitleLabel = UILabel(text: "subtitleLabel")
let imageView = UIImageView()
var body: Layout {
HStack {
VStack(alignment: .leading) {
titleLabel
Spacer(4)
subtitleLabel
imageView.resizable()
}
Spacer()
}
.padding(.horizontal, 16)
.padding(.vertical, 8)
}
} |
Answered by
constantine-fry
Dec 1, 2025
Replies: 2 comments 1 reply
|
Hi @zeroskylian, thanks for trying QuickLayout. There are multiple way of achieving what you need:
class ViewController: UIViewController {
let cellView = MyCellView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(cellView)
}
var body: Layout {
ZStack {
cellView
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
body.applyFrame(view.bounds.inset(by: view.safeAreaInsets))
}
}You also can achieve that without QuickLayout as well just call the class ViewController: UIViewController {
let cellView = MyCellView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(cellView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let cellSize = cellView.sizeThatFits(view.bounds.inset(by: view.safeAreaInsets).size)
cellView.frame = CGRect(origin: .zero, size: cellSize)
}
}Let me know if you have any further questions. Cheers! |
1 reply
Answer selected by
constantine-fry
|
This is a very good question. Let me convert it to a discussion so other people can find it in the future. This issues will be closed and locked, but feel free to keep the conversation in the discussion. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @zeroskylian, thanks for trying QuickLayout. There are multiple way of achieving what you need:
bodyand callbody.applyFramein theviewDidLayoutSubviews.You also can achieve that without QuickLayout as well just call the
sizeThatFitsand set the returned size on cellView.