1

So, I thought I'm quite familiar with Swift, but it turns out, I'm not.

I'm trying to create UINavigationController and UIViewController extensions and override some methods to simplify control over device rotation.

So, I found that it's still possible in Swift: https://stackoverflow.com/a/28220616/982550 But as I don't want to create base UIViewController class I thought I follow same approach as in Obj-C:

extension UINavigationController {

public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return topViewController!.supportedInterfaceOrientations()
}

public override func shouldAutorotate() -> Bool {
    return topViewController!.shouldAutorotate()
}

override public func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return topViewController!.preferredInterfaceOrientationForPresentation()

    }
}

extension UIViewController {

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

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

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

But it doesn't compile with XCode 7.2.1 and Swift 2.1, I got:

: Method 'supportedInterfaceOrientations()' with Objective-C selector 'supportedInterfaceOrientations' conflicts with previous declaration with the same Objective-C selector

I guess there is no way around it, as both class are subclasses of UIViewController I can't make 2 extensions with same methods?

Community
  • 1
  • 1
Krodak
  • 1,493
  • 14
  • 21
  • Why dont you just extend UIViewController, So maybe create a KrodakLayoutViewController: UIViewController. Then add the functions above and use this controller for your views? then each one would have the functionality you require and you just extend it again – Scriptable Feb 08 '16 at 17:31
  • As I stated in my question, I'm looking for the way to solve this one without using subclasses. I already solved this as a problem in the application (using one extension for `UIViewController` and checking if it's instance of `UINavigationController`), but I'm curious about reasons for this error and any ways to 'bypass' it. – Krodak Feb 08 '16 at 17:39

0 Answers0