How to remove all navigationbar back button title

2019-01-30 17:48发布

When I push a UIViewController, it has some title in back button at new UIViewController, if the title has a lot of text, It does not look good in iPhone 4s So I want to remove it.

If I add some code in prepareForSegue function, it is going to be a trouble.

Any better way to achieve this?

21条回答
2楼-- · 2019-01-30 17:53

Swift 3:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
查看更多
小情绪 Triste *
3楼-- · 2019-01-30 17:53

Swift 4

Just copy this code in didFinishLaunchingWithOptions launchOptions

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000.0, 0.0), for: .default)
查看更多
Evening l夕情丶
4楼-- · 2019-01-30 17:55

If you want back arrow so following code put into AppDelegate file into didFinishLaunchingWithOptions method.

For Objective-C

 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

For Swift

let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)

Another option give below.

In Objective C

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

In Swift

self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)

UPDATE :

    let BarButtonItemAppearance = UIBarButtonItem.appearance()

    let attributes: [NSAttributedStringKey: Any] = [
    BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
            NSAttributedStringKey.font: UIFont.systemFont(ofSize: 0.1),
            NSAttributedStringKey.foregroundColor: UIColor.clear]

    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

UPDATE SWIFT 4.1 :

    let attributes = [NSAttributedStringKey.font:  UIFont(name: "Helvetica-Bold", size: 0.1)!, NSAttributedStringKey.foregroundColor: UIColor.clear]

    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

Using Offset

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, 0), for:UIBarMetrics.default)

So may be your problem has been solve.

Happy coding.

查看更多
地球回转人心会变
5楼-- · 2019-01-30 17:56

Just use this:

func removeBackButton(vc:UIViewController) {
        let button = UIButton.init(type: .custom)
        button.setImage(UIImage.init(named:""), for: .normal)
        let leftBarButton = UIBarButtonItem.init(customView: button)
        vc.navigationItem.leftBarButtonItem = leftBarButton
}

So call this method in viewDidLoad:

override func viewDidLoad() {
        super.viewDidLoad()
     removeBackButton(vc:self)
}
查看更多
Juvenile、少年°
6楼-- · 2019-01-30 17:57

I would like to share a solution that works for me. Also, it can be adjusted base on your needs and requirements.

Note, in my case, I use a storyboard to specify CustomNavigationBar

Swift 4.2

class CustomNavigationBar: UINavigationBar {

    override func awakeFromNib() {
        super.awakeFromNib()
        guard let topItem = topItem else { return }
        removeBackButtonTitle(for: topItem)
    }

    override func pushItem(_ item: UINavigationItem, animated: Bool) {
        removeBackButtonTitle(for: item)
        super.pushItem(item, animated: animated)
    }

    func removeBackButtonTitle(for item: UINavigationItem) {
        item.backBarButtonItem = UIBarButtonItem()
    }
}
查看更多
冷血范
7楼-- · 2019-01-30 17:58

I usually add or change the back button in viewDidLoad of the UIViewController.

Something like that should work:

let leftButton = UIBarButtonItem(title: "Back", style:     UIBarButtonItemStyle.Plain, target: self, action: "closeView:")
self.navigationItem.leftBarButtonItem = leftButton

Don't forget to change and implement the function that it's called to close the view.

Even easier, just change the title:

self.navigationItem.leftBarButtonItem.title = "Back"
查看更多
登录 后发表回答