将 UIBarButtonItem 添加到 navigationItem.titleView 中?
我有一个带有导航栏的导航控制器。我真的很想要 3 个 UIBarButtonItems,一个在左边,一个在中间,一个在右边。我能够添加左侧和右侧的项目,但是如何在中间添加一个项目,因为使用导航控制器时,我无法将项目数组添加到 navBar items 属性中?
我可以以某种方式在 titleView 位置添加一个 UIButton,其样式类似于 UIBarButtonItem 吗?
I have a navigationController which has a navigation bar. I would really like to have 3 UIBarButtonItems, one on the left, one in the middle, and one on the right. I am able to get the left and right ones added, but how would I add one in the middle, since when using a navigationController, I can't add an array of items to the navBar items property?
Could I somehow add a UIButton, styled like a UIBarButtonItem in the titleView location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取自苹果api,基本上你可以创建一个自定义的 UIView,其中有一个 UIButton 并将其用作你的 titleView (注意有关 leftBarButtonItem 的注释会导致 titleView 被忽略并且不显示):(编辑注释:这是一个属性
UINavigationItem
)titleView
当此项为顶部项时,在导航栏中央显示的自定义视图。
@property(非原子,保留) UIView *titleView
讨论
如果此属性值为 nil,则当该导航项是顶部项时,导航项的标题将显示在导航栏的中央。如果将此属性设置为自定义标题,则会显示该属性而不是标题。如果 leftBarButtonItem 不为零,则忽略此属性。
自定义视图可以包含按钮。使用 UIButton 类中的 buttonWithType: 方法以导航栏的样式将按钮添加到自定义视图。自定义标题视图位于导航栏的中心,并且可以调整大小以适应。
taken from apple api, basically you can create a custom UIView that has a UIButton in it and use this as your titleView (notice the note about a leftBarButtonItem causes the titleView to be ignored and not shown): (edit note: this is a property of
UINavigationItem
)titleView
A custom view displayed in the center of the navigation bar when this item is the top item.
@property(nonatomic, retain) UIView *titleView
Discussion
If this property value is nil, the navigation item’s title is displayed in the center of the navigation bar when this item is the top item. If you set this property to a custom title, it is displayed instead of the title. This property is ignored if leftBarButtonItem is not nil.
Custom views can contain buttons. Use the buttonWithType: method in UIButton class to add buttons to your custom view in the style of the navigation bar. Custom title views are centered on the navigation bar and may be resized to fit.