Orientation change in Swift height change and uibutton hide on landscape - iphone

I am making one custom keyboard in which when orientation of mobile is changed then in landscape i have to hide one button please suggest how can i do this i am using below code to do this task please help me.
in landscape also the button is visible i want to hide button on landscape and visible in portrait mode
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
var currentDevice: UIDevice = UIDevice.currentDevice()
var orientation: UIDeviceOrientation = currentDevice.orientation
if orientation.isLandscape {
button.hidden = true
}
if orientation.isPortrait {
button.hidden = false
}
}

In your viewDidLoad put
NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged", name: UIDeviceOrientationDidChangeNotification, object: nil)
Then add this method
func orientationChanged()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
button.hidden = true
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
button.hidden = false
}
}
Note:
UIDevice.currentDevice().orientation might not give you the correct orientation. You can use the status bar orientation instead UIApplication.sharedApplication().statusBarOrientation
From the apple docs:
UIDevice.currentDevice().orientation
The value of the property is a constant that indicates the current
orientation of the device. This value represents the physical
orientation of the device and may be different from the current
orientation of your application’s user interface. See
“UIDeviceOrientation” for descriptions of the possible values.

Related

How can I enable split view and control the screen orientation?

Somebody else asked the same question in 2018, but he/she did not get answer so hopefully I will have more chance. :-)
Basically, I need my app to have the split view enabled. To do so the target deployment info should be:
All the 'Device orientation' selected
The 'Requires full screen' not selected
To have the possibility to control the orientation, you need the target deployment information to be:
The 'Device orientation' needs at least 'Portrait' OR 'Portrait + Landscape left + Landscape right'
The 'Require full screen' needs or does not need to be selected.
Like you can see, I did not put the 'Upside Down' option from 'Device Orientation' because doing so, I can't lock the orientation for a specific view controller.
If you think it is possible, what am I missing?
Here the code that I am using to lock the orientation based on this:
AppOrientationUtility
struct AppOrientationUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
And using it:
override func viewDidLoad() {
super.viewDidLoad()
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
}

How to present a UIView or UIViewController in a specific orientation only? (regardless of the device orientation)

I have an app whose root view controller is in the portrait mode. But I want to fix one of its child view controller to landscapeLeft. How can I realize this?
My project setting include both landscapeLeft and portrait orientations for the app.
In root view controller:
override var shouldAutorotate: Bool {
return false
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation
{
return .portrait
}
In the child view controller:
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation
{
return .landscapeLeft
}
But it seems that the child view controller is still fixed at the portrait mode, maybe dominated by the setting in the root view controller? If so, how should I adjust to realize my goal?
Hope some one can provide insights here. Just want to make sure I'm looking at the right place.
Thanks a lot!
Regards,
Paul
Go in info.plist and under "supported interface orientations" delete everything except landscape (left home button) or landscape (right home button). Which ever you prefer.

Swift iOS -How to Add Subview to Window's Center?

I have an activity indicator that gets presented on an iPhone and iPad. In the iPad in split screen mode it gets presented to whichever side of the view that called it. I would instead like it to get presented in the middle/center the window's screen. If I do it this way wether on the iPhone in portrait or iPad in split screen mode it will always be in the center of the screen.
How do I do this?
MyView: UIViewController{
let actInd = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
#IBAction fileprivate func buttonPressed(_ sender: UIButton) {
guard let window = UIApplication.shared.keyWindow else { return }
//how to add actInd as subview to the window' screen?
actInd.startAnimating()
}
}
It's pretty simple. Turn off the auto-resizing mask. Add the add actInd to window, then set the center anchors.
actInd.translatesAutoresizingMaskIntoConstraints = false
window.addSubview(actInd)
actInd.centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true
actInd.centerYAnchor.constraint(equalTo: window.centerYAnchor).isActive = true
Window is subclass of UIView. Just add it as it's subview like you're adding a view to another view. But remember that window is shared throughout your app, so adding it every-time will consume memory, remove it after your job is done.
If you want to center it in the window, you can use autoResizingMask or add constraints to it.

SpriteKit default landscape orientation

I am making a SpriteKit game that runs in both landscape modes. How can I pick the default landscape orientation?
When I first load the game it loads in landscapeLeft (home button is on left)
Looking at most games on the app store it seems the default orientation is the other way around (landscape right). I also prefer it to be that way.
I have tried playing around with the info.plist but couldnt find anything.
I dont see what i can change in my viewController either because I want to keep both landscape orientations.
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .Landscape
} else {
return .Landscape
}
}
Anyone know what I have to do?
Thanks in advance
Just change the order of items for Supported Interface Orientation key inside info.plist. The app will launch in whatever orientation is specified in the first item. By default, that would be landscape left. Just drag Item 1 (landscape right) in the place of Item 0 (landscape left):
You can set this in the View Controller by overriding:
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeLeft
}
Just return the orientation you want for the game View Controller.

Swift PLUICameraViewController shouldAutorotate] is returning YES'

I have my app in only landscape and with this code i take a picture
#IBAction func shootPhoto(sender: UIBarButtonItem){
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
presentViewController(picker, animated: true, completion: nil)
} else {
noCamera()
}
}
but when I press the button I get this error
'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [PLUICameraViewController shouldAutorotate] is returning YES'
According to Apple's documentation UIImagePickerController:
IMPORTANT
The UIImagePickerController class supports portrait mode only. This
class is intended to be used as-is and does not support subclassing.
The view hierarchy for this class is private and must not be modified,
with one exception. You can assign a custom view to the
cameraOverlayView property and use that view to present additional
information or manage the interactions between the camera interface
and your code.
You have to check Portrait Device Orientation in your targets General settings under Deployment Info.