SpriteKit default landscape orientation - swift

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.

Related

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.

iOS 9: view is not rendered properly after I rotate device

This is my custom UIView:
After I rotate to horizontal, and then back to Portrait it looks like this:
Why?
After I rotate device this method is called:
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
tableView.reloadData()
}
What Am I doing wrong? It works perfect for ios8, but not for ios9

IOS 8 - After orientation change, Navigation bar is obscured by the status bar

I'm using SWRevealViewController and my app is locked on portrait orientation. On one of my view controllers I'm launching a youtube player using the YTPlayerView. When displaying the video I switch the orientation to landscape programmatically like so:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
and when exiting the video, back to portrait like so:
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
All works fine, but introduce a layout issue: the Navigation bar Y is not offsetted by the status bar, resulting the status bar to be on top of the Navigation bar (see screens).
Before orientation change
Playing video on landscape orientation
Navigation bar is being obscured by the status bar
What am I missing here :) ?
Declare a var that will cache initial navigation bar size
var navigationFrame = CGRect()
Then, in viewDidLoad save the default value
override func viewDidLoad() {
if let navFrame = self.navigationController?.navigationBar.frame {
navigationFrame = navFrame
}
}
After changing orientation call this method to solve to problem:
self.navigationController?.navigationBar.frame = navigationFrame
This solution is inspired from #Shlomi

Orientation change in Swift height change and uibutton hide on landscape

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.