Chris Byatt Chris Byatt - 28 days ago 13
Swift Question

Set all back button titles in UINavigationController

I'm trying to be clever about setting all

title
properties of the the "Back" buttons in a UINavigationController so that I don't have to do
self.navigationController.navigationBar.backButtonItem.title = "Back"
everywhere or subclass a
UINavigationController
and set it everywhere, so I've created this extension:

extension UINavigationItem {
open var backBarButtonItem: UIBarButtonItem? {
get {
return self.backBarButtonItem
}

set {
newValue?.title = "Back"
backBarButtonItem = newValue?
}
}
}


But it says
'backBarButtonItem' used within its own type
.

Has anybody done this before or can think of a way to make it work?

Answer

You are getting this error because you cannot create a variable with the name which is similar to those variables which are defined in the SDK.

You can't override the existing functionality

Like in your case you are naming it as backBarButtonTitle which is defined as open var backBarButtonItem: UIBarButtonItem? in UINavigationBar class of UIKit

As it is mentioned in doc of Apple

Extensions can add new functionality to a type, but they cannot override existing functionality.