Chris Byatt Chris Byatt - 1 year ago 116
Swift Question

Set all back button titles in UINavigationController

I'm trying to be clever about setting all

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
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download