添加按钮导航栏中心

add button navigationbar center

在导航栏上添加左右buttonbaritem很容易

但我希望添加按钮导航栏居中

我的密码是

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

但什么也没发生

欢迎评论

根据 UINavigationItem 的 titleView 属性 的文档,"Use the buttonWithType: method in UIButton class to add buttons to your custom view in the style of the navigation bar."

默认情况下 titleViewnil,因此您的代码不会执行任何操作。

将代码更改为:

self.navigationItem.titleView = button;

这会将您的按钮设置为 titleView