add button navigationbar center

2k views Asked by At

It is easy to add left and right buttonbaritem on navigationbar

but I hope to add button navigationbar center

my code is

 [self.navigationItem.titleView addSubview:button ];//button is not nil

but nothing happens

Yourc comment welcome

2

There are 2 answers

2
rmaddy On BEST ANSWER

The titleView is nil by default so your code doesn't do anything.

Change the code to:

self.navigationItem.titleView = button;

This sets your button as the titleView.

0
dudeman On

According to the documentation for UINavigationItem's titleView property, "Use the buttonWithType: method in UIButton class to add buttons to your custom view in the style of the navigation bar."