I'm writing a function which can get the height and width of the container view:
// container view's UIViewController
class SelectionView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func getBounds -> (CGFloat,CGFloat){
let x = self.view.bounds.width / 5
let y = self.view.bounds.height / 15
return x,y
}
}
I write a button to call this getBounds() and it works well, but when I put it in the viewDidLoad() function
override func viewDidLoad() {
super.viewDidLoad()
getBounds()
}
getBounds() returns me different height and width and it is clearly not the bounds of this container view.
I'm pretty sure I've linked this class to the container view!
View layout is not setup in viewDidLoad. Therefore any resizing is not done yet and your size is wrong(probably the same as declared in Storyboard/Xib).
Move getBounds in viewWillLayoutSubviews or viewWillAppear and it will work correctly. Please mind that those method won't be called one time only ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With