How to set device (UI) orientation programmatically?
That method is called to determine whether your interface should automatically rotate to a given rotation (i.e letting UIKit do the hard work, rather than you doing it manually).
So if you wanted your app to only work in landscape you'd implement the body of that method with:
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
If you wanted your UI to auto rotate to all orientations you could just
return YES;
Is that what you were asking?
How to change Device Orientation programmatically in Swift?
For each VC declare this variable w/ desired orientation. This is for portrait.
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait }
Then on appearance enforce the desired orientation.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.setAnimationsEnabled(false)
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIView.setAnimationsEnabled(true)
}
Setting device orientation in Swift iOS
You can paste these methods in the ViewController of each view that needs to be portrait:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
programmatically setting orientation in viewdidload is not working
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
This method is deprecated. You cannot use this method anymore.
https://stackoverflow.com/a/12813644/1405008
Above link gives detail on how to provide portrait only mode for UIViewController
.
If you pushed into NavigationViewController
then try this.
https://stackoverflow.com/a/16152506/1405008
How to update the UI when the device orientation changed in Swift?
Actually by using
loginView.frame = CGRect(origin: .zero, size: view.bounds.size)
you assign a frame to your login view which will not change on device rotation.
you might wanna use a constraint approach here as well:
// try replacing
// loginView.frame = CGRect(origin: .zero, size: view.bounds.size)
// with
loginView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
loginView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
loginView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
loginView.topAnchor.constraint(equalTo: view.topAnchor),
loginView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
Another approach would be updating the frame of your login view e.g.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
loginView.frame = CGRect(origin: .zero, size: view.bounds.size)
}
Last but not least you can use autoresizingMask in viewDidLoad
loginView.frame = CGRect(origin: .zero, size: view.bounds.size)
loginView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
P.S. My previous comment was misleading since the login frame wasn't set in the setupStackView() function
and my final comment:
// you can replace
loginView.frame = CGRect(origin: .zero, size: view.bounds.size)
// with
loginView.frame = view.bounds
Related Topics
Why Nsuserdefaults Failed to Save Nsmutabledictionary in Ios
How to Convert from Degrees to Radians
Get a List of All Contacts on Ios
Shouldautorotatetointerfaceorientation Not Being Called in iOS 6
Enabling Auto Layout in iOS 6 While Remaining Backwards Compatible With iOS 5
How to Perform Wireless Debugging in Xcode 9 With iOS 11, Apple Tv 4K, etc
Draw Segments from a Circle or Donut
Ios: How to Change App Language Programmatically Without Restarting the App
Positioning Mkmapview to Show Multiple Annotations at Once
How to Load Storyboard Programmatically from Class
How to Catch "Index Out of Range" in Swift
How to Tell Swiftui Views to Bind to Nested Observableobjects
Storyboard Doesn't Contain a View Controller With Identifier
How to Load a Uiimage from a Url
How to Set the Width of a Cell in a Uitableview in Grouped Style