I have a trouble with loading view controller from xib
I have a xib file which contains a view controller and a subview inside it as the image below. Notice that I'm using iPhone 11 Pro Max
in xib (which have screen width = 414)
The subview has 8 leading and trailing to its parent.
The problem is when i run the app on iPhone 12 Pro Max (which have screen width = 428), I check values of screen size and the subview size and it returns strange values. Here is my code:
public override func viewDidLoad() {
super.viewDidLoad()
print(view.frame) // (0.0, 0.0, 414.0, 896.0)
print(collageView.frame) // (8.0, 210.0, 398.0, 398.0)
}
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(view.frame) // (0.0, 0.0, 428.0, 926.0) updated
print(collageView.frame) // (8.0, 210.0, 398.0, 398.0) ??
let width = collageView.frame.width
let height = collageView.frame.height
let frames = self.frames?(CGSize(width: width, height: height)) ?? []
for i in 0..<frames.count {
let componentView = CropComponentView(frame: frames[i])
componentView.image = images[i]
collageView.addSubview(componentView)
}
}
My questions are:
How can we get the properly screen size inside viewDidLoad ? why the subview's frame doesn't update like screen size does ?
viewDidLoad
. It's too early. programmingios.net/premature-layout – mattlayoutIfNeeded()
and then check frame in viewdidload. Let me know either it works or not – dahiya_boy