Supportedinterfaceorientationsforwindow in Swift 2.0

supportedInterfaceOrientationsForWindow in Swift 2.0

Try new syntax:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return [.Portrait, .PortraitUpsideDown]
}

How to lock orientation just for one view controller?

This code should work:

    override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

override func shouldAutorotate() -> Bool{
return false
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}

If it is now working for you, then I suppose your controller is in some another controller(UINavigationController, UITabBarController, UISplitViewController). In this case you need to use this code in that parent controller.

If your navigation controller contain more than one view controller, and you need to disable orientation only for some of them, then you need to inherit UINavigationController class and write there something like:

class NavigationController: UINavigationController {

var shouldRotate: Bool = true

override func supportedInterfaceOrientations() -> Int {
return shouldRotate ? Int(UIInterfaceOrientationMask.Portrait.rawValue) : Int(UIInterfaceOrientationMask.All.rawValue)
}

override func shouldAutorotate() -> Bool{
return shouldRotate
}
}

Then in controller that you need to disable orientation you can disable it for your navigation controller:

class ViewController: UIViewController {

var lastControllerRotationStatus: Bool?

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

if let navigationController = self.navigationController as? NavigationController {
lastControllerRotationStatus = navigationController.shouldRotate
navigationController.shouldRotate = false
}
}

override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)

if let navigationController = self.navigationController as? NavigationController {
navigationController.shouldRotate = lastControllerRotationStatus ?? true
}
}
}

But remember, that you need to restore old rotation status after your controller will be pushed out navigation controller. In this example I'm saving rotation status before changing it, and restoring it after controller disappeared. Also you can use some other approach. For example you can overload UINavigationController method popViewController, and there set shouldRotate to false.

The same approach with variable setting from controller you can use for controlling rotation from AppDelegate method application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int

Then you do not need to inherit from navigation controller.

Your AppDelegate class code will look like:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {

return shouldRotate ? Int(UIInterfaceOrientationMask.All.rawValue) : Int(UIInterfaceOrientationMask.Portrait.rawValue)

}

}

And your controller code will look like:

class ViewController: UIViewController {

var lastControllerRotationStatus: Bool?

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
lastControllerRotationStatus = appDelegate.shouldRotate
appDelegate.shouldRotate = false
}
}

override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = lastControllerRotationStatus ?? true
}
}
}

Use NSSerialization.datawithJSON in Swift 2

let options = prettyPrinted ? 
NSJSONWritingOptions.PrettyPrinted : NSJSONWritingOptions(rawValue: 0)

is the right syntax for swift 2.0

Error combining NSCalendarUnit with OR (pipe) in Swift 2.0

NSCalendarUnit is an OptionSetType in Swift 2, instead of a RawOptionSetType. This means that you can't logical-or it anymore. Instead, you can use an array literal representation of it:

formatter.allowedUnits = [.Year, .Month, .WeekOfMonth, .Day, .Hour, .Minute]

Force landscape mode in one ViewController using Swift

It may be useful for others, I found a way to force the view to launch in landscape mode:

Put this in the viewDidLoad():

let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

and,

override var shouldAutorotate: Bool {
return true
}

Unable to force UIViewController orientation

try this for force to LandscapeRight mode only

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

if(self.supportedInterfaceOrientations() == UIInterfaceOrientationMask.LandscapeRight && UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
{
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

}

and then use a category like this

    import UIKit

extension UINavigationController{

override public func shouldAutorotate() -> Bool
{
return (self.viewControllers.last?.shouldAutorotate())!
}

override public func supportedInterfaceOrientations() ->UIInterfaceOrientationMask
{
return (self.viewControllers.last?.supportedInterfaceOrientations())!;
}

override public func preferredInterfaceOrientationForPresentation()-> UIInterfaceOrientation
{
return (self.viewControllers.last?.preferredInterfaceOrientationForPresentation())!;
}

}

EDITED

If you are not using navigation controller use this

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

if(self.supportedInterfaceOrientations() == UIInterfaceOrientationMask.LandscapeRight && UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
{
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

}

override func supportedInterfaceOrientations() ->UIInterfaceOrientationMask
{
return .LandscapeRight;
}

I hope this helps you

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
}

Check if UIDatePicker.date is nil in Swift

datepicker.date is of type NSDate, not NSDate?. (see documentation) That's why you cannot check if it is nil because it is not an optional.

If you want see if it is still spinning refer to this question: Detect if UIDatePicker is rolling?



Related Topics



Leave a reply



Submit