I am making a custom keyboard. I want to generate the keys'(UIButtons) width and height based on the view's width and height.
When the keyboard is loaded initially, viewDidAppear correctly determines the height of the view. (375 x 216)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.renderKeys()
}
When rotating to Landscape, it invokes viewWillTransition
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
NSLog("toSize \(size.width) x \(size.height)")
coordinator.animate(alongsideTransition: {(_ context: UIViewControllerTransitionCoordinatorContext) -> Void in
NSLog("animating \(self.view.frame.width) x \(self.view.frame.height)")
}, completion: {(_ context: UIViewControllerTransitionCoordinatorContext) -> Void in
NSLog("animationCompleted \(self.view.frame.width) x \(self.view.frame.height)")
self.renderKeys()
})
}
Rotating back to Portrait results this.
What I found is that viewWillTransition's to size isn't correctly determining the size that the view will be or I am just misunderstanding the usage of the function. Even after the animation, I am not able to get right width and height of the view.
Here is the debug log in order.
[28651:1348399] Calling viewDidAppear
[28651:1348399] Cleaning keys
[28651:1348399] toSize 667.0 x 216.0
[28651:1348399] animating 667.0 x 216.0
[28651:1348399] animationCompleted 667.0 x 216.0
[28651:1348399] Cleaning keys
[28651:1348399] toSize 375.0 x 162.0
[28651:1348399] animating 375.0 x 162.0
[28651:1348399] animationCompleted 375.0 x 162.0