Reputation: 1
I am trying to create a custom keyboard in swift programmatically and have so far been able to create the constraints I want for portrait and landscape on iPhone, but only when I start the keyboard in the respective orientation.
The problem comes when I flip the device myself: Portrait, Landscape. I have stored my constraints in two arrays and am running this block of code in my viewDidLoad and viewWillTransition functions:
if UIScreen.main.bounds.height >= 568 {
NSLayoutConstraint.deactivate(constraintsLandscape)
NSLayoutConstraint.activate(constraintsPortrait)
}
else if UIScreen.main.bounds.height <= 414 {
NSLayoutConstraint.deactivate(constraintsPortrait)
NSLayoutConstraint.activate(constraintsLandscape)
}
Any help with this is appreciated.
Current portrait and landscape
Upvotes: 0
Views: 141
Reputation: 111
if this is inside a method, do u call it after the screen rotates ? if dont you can check device rotation overriding this method:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
print("Device is in Landscape mode")
NSLayoutConstraint.deactivate(constraintsPortrait)
NSLayoutConstraint.activate(constraintsLandscape)
} else {
print("Device is in Portrait mode")
NSLayoutConstraint.deactivate(constraintsLandscape)
NSLayoutConstraint.activate(constraintsPortrait)
}
}
if you are already doing this, then you just need to call self.view.layoutifNeeded()
after making changes to your constraints.
Upvotes: 1