Set custom navigation bar class in UINavigationController
Asked Answered
L

2

7

I have a custom navigationBar :

class Name_UINavigationBar: UINavigationBar {
    // code
}

and I want to set it to my navigation controller programmatically. So I tried :

var navController : UINavigationController = UINavigationController(
            navigationBarClass: object_getClass(Name_UINavigationBar),
            toolbarClass: nil)
// code

But it crash saying :

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
    reason: 'MyProject.Name_UINavigationBar is not a subclass of UINavigationBar'
Lonnylonslesaunier answered 9/9, 2014 at 10:29 Comment(0)
L
7

Try this.

let navController = UINavigationController(navigationBarClass: YourNavigationBar.self, toolbarClass: nil)

I encountered the same error and was able to get it working again like that.

Lura answered 31/10, 2014 at 7:26 Comment(0)
C
0

In Swift there is no supported way to get the Class of an object. You're problem is almost certainly to do with the object_getClass method which I believe is no longer supported as of Beta 5.

I imagine APIs like this will either be updated, rewritten or simply deprecated soon. For now you will need to do it in Objective-C.

Catatonia answered 9/9, 2014 at 10:54 Comment(1)
I'm on xcode 6 beta 7 and it doesn't say anything about object_getClass. But what you say seems to be a good explanation.Lonnylonslesaunier

© 2022 - 2024 — McMap. All rights reserved.