添加到 navigationItem.titleView 时的 UIButton 样式
我需要向 UINavigationItem titleView
添加一个按钮(实际上是 UINavigation Bar 的中心。
我使用以下代码来完成此操作:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 70, 30);
[btn setSelected:YES];
[btn setTitle:@"list" forState:UIControlStateNormal];
并且我执行以下操作:
我需要为“完成”按钮(即 UIBarButtonItem
)提供相同的样式按钮 “列表”?
I need to add a button to the UINavigationItem titleView
(actually to the center of the UINavigation Bar.
I've used the following code to accomplish so:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 70, 30);
[btn setSelected:YES];
[btn setTitle:@"list" forState:UIControlStateNormal];
And I've go the following:
I need to give the same style of the "Done" button (which is UIBarButtonItem
) to the button "list"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 UISegmentedControl:
You can use UISegmentedControl:
UIBarButtonItems 只能用作 UIToolbar 中的项目(它们实际上不是 UIView 的实例),因此在 titleView 中直接使用 UIBarButtonItem 的唯一方法是将 UIToolbar 的实例设置为 titleView 并将 barButtonItem 添加到该实例工具栏。但是,我认为这不会起作用。
我认为您需要做的是找到一种使用普通旧 UIButton 来模仿 UIBarButtonItem 样式的方法。您可以使用 UIKit 图稿提取器获取用于创建 UIBarButtonItems 的实际图像文件。然后,只需使用该背景图像创建自定义 UIButton 即可,然后就可以开始了。
UIBarButtonItems can only be used as items in a UIToolbar (they're not actually instances of UIView), so the only way to directly use a UIBarButtonItem in your titleView would be to set an instance of UIToolbar as your titleView and add your barButtonItem to that toolbar. However, I don't think that will work.
I think what you'll need to do is find a way to mimic the UIBarButtonItem style using a plain old UIButton. You can get the actual image file used to create UIBarButtonItems using the UIKit artwork extractor. Then it's just a matter of creating a custom UIButton using that background image, and you're good to go.
我使用了 andrej 的基于 UISegmentedControl 的方法,并将其扩展了一点,使其表现得更像普通的 UIButton:
对于基本用法,您可以将其用作 UIButton 的替代品:
该事件仅在触摸发生在分段控制。享受。
I have used andrej's UISegmentedControl-based approach and expanded it a bit to behave more like a normal UIButton:
For basic usage, you can use it as a UIButton drop-in replacement:
The event will only fire when the touchup happened inside the bounds of the segmented control. Enjoy.
是的,您也应该使用 UIBarButtonItem 作为
使用自定义标识符和标题属性的按钮“列表”。
您可以使用 UIBarButtonItem 标识符“灵活空间”在中心显示您的按钮“列表”。
Yes, you should use a UIBarButtonItem too for the button "list"
using custom identifier and a title properties.
You can use the UIBarButtonItem identifier "flexible space" to display your button "list" at the center.