I have a viewController, I'm using anchors and I want to get the frame of a button. I only want the frame when the view is pushed on so I use isMovingToParentViewController
. Like this:
if isMovingToParentViewController {
let myButtonFrame = myButton.convert(myButton.bounds, to: self.view)
}
When I add it in viewWillAppear
the code to get the button's frame runs but when I add the same code to viewDidLayoutSubviews
it doesn't run.
Why is that?
Just to be clear in viewDidLayoutSubviews
when I add a break point it does hit if isMovingToParentViewController
but the code inside of it never gets hit.
When the view gets pushed on isMovingToParentViewController does get hit, when it pops isMovingToParentViewController doesn't get hit.
override func viewDidLoad() {
super.viewDidLoad()
// anchors are set here but the frames haven't been set yet
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isMovingToParentViewController {
// this code runs
let myButtonFrame = myButton.convert(myButton.bounds, to: self.view)
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if isMovingToParentViewController {
// this code NEVER runs
let myButtonFrame = myButton.convert(myButton.bounds, to: self.view)
}
}
The header documentation states that
isMovingToParentViewController
and the 3 other methods are only valid in appearance callbacks likeviewWillAppear
thus not in layout callbacks likeviewDidLayoutSubviews
.