Carl Carl - 3 months ago 23
Swift Question

What is the return value of navigationControllerSupportedInterfaceOrientations in Swift?

It is a delegate method of

UINavigationControllerDelegate
. The return value in Swift and Objective-C is different. see:

Obj-C:

- (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController


Swift

func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> Int


When i use objective-c, it works fine. However, when i use Swift, i cannot return correct value in the method. See the picture below:
enter image description here

Can somebody explain me why and is there any solution´╝č

Answer

Try Using This

func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController!) -> Int 
{
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

For more details click here

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch06p281navigationInterface/ch19p615navigationInterface/ViewController.swift

The reason of casting is in Objective-C

The above function return type is NSInteger, because UIInterfaceOrientationPortrait is a enum of type NSInteger.

And Swift it is define in the form of struct.

struct UIInterfaceOrientationMask : RawOptionSetType {
init(_ rawValue: UInt)
init(rawValue: UInt)

static var Portrait: UIInterfaceOrientationMask { get }
static var LandscapeLeft: UIInterfaceOrientationMask { get }
static var LandscapeRight: UIInterfaceOrientationMask { get }
static var PortraitUpsideDown: UIInterfaceOrientationMask { get }
static var Landscape: UIInterfaceOrientationMask { get }
static var All: UIInterfaceOrientationMask { get }
static var AllButUpsideDown: UIInterfaceOrientationMask { get }
}

https://books.google.co.in/books?id=5baVBQAAQBAJ&pg=PA277&lpg=PA277&dq=reason+of+UIInterfaceOrientationMask.Portrait.rawValue+using+rawValue&source=bl&ots=3X2wXD9HQc&sig=1Knh1LPBj2aoRMwuYAHUFjGdHxw&hl=en&sa=X&ei=0Z_6VPizEs7VuQTJz4KYBA&ved=0CC8Q6AEwAw#v=onepage&q=reason%20of%20UIInterfaceOrientationMask.Portrait.rawValue%20using%20rawValue&f=false

Comments