How to set @QuickLayout view frame in UIViewController? #8
-
|
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)
}
} |
Beta Was this translation helpful? Give feedback.
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! |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
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.