普通样式的 UINavigationBar BarButtonItem
我得到以下代码:
- (id)init {
if (self = [super init]) {
self.title = @"please wait";
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"star.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonFavoriteClicked:)];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
但我的按钮看起来仍然像带有 UIBarButtonItemStyleBordered 的按钮
有没有办法在这个位置设置一个普通样式的按钮?
i got the following code:
- (id)init {
if (self = [super init]) {
self.title = @"please wait";
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"star.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonFavoriteClicked:)];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
but my button looks still like a Button with UIBarButtonItemStyleBordered
is there a way to set a button with plain style at this position?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
试试这个,
Try this,
在界面生成器中创建一个 UIButton,使其看起来完全符合您的要求。在 .h 中创建一个插座:
然后使用自定义视图将其设置为右栏按钮项,这将消除边框:
这有效,因为 UIButton 是 UIView 的子类。
Create a UIButton in interface builder, make it look like exactly what you want. Create an outlet for in in your .h:
Then set it as your right bar button item using a custom view, which will eliminate the border:
This works because UIButton is a subclass of UIView.
试试这个:
希望有帮助。
Try this instead:
Hope it helps.
或者在没有 IB 的代码中执行此操作:
结果:
当使用此图标图像时(它是白色的)背景所以在这里不可见 - 链接是: https://i.sstatic.net/lk5SB.png< /a>):
Or do this in code without IB:
Results in:
When using this icon image (it's white on a white background so isn't visible here - link is: https://i.sstatic.net/lk5SB.png):
这很奇怪,但确实有效。对图像/渠道说“不”!您甚至不应该设置 UIBarButtonItemStylePlain 属性。诀窍是将按钮放入 UIToolBar 中并具有一些特殊属性:
It's weird but it works. Say "No" to images/outlets! You shouldn't even set the UIBarButtonItemStylePlain property. The trick is to place button into UIToolBar with some special attributes:
虽然 Frank 是对的,但这段代码应该在
viewDidLoad
中,这里的问题是 BarButtons 的样子。如果你想让它看起来不同,你需要使用不同类型的 UIView。您可以将 UIButton 添加到 UIToolbar 中。编辑:
抱歉,您想要一个普通的 UIBarButtonItem。我不相信你可以用 UINavigationBar 做到这一点。您可以尝试添加 UserInteractionEnabled UILabel 作为 rightBarButtonItem,我不确定它是否有效。
目前看来这是不可能的。sbwoodside 有一个解决方案。
Although Frank is right, this code should be in
viewDidLoad
, the issue here is what BarButtons look like. If you want it to look different, you need to use a different type of UIView. You can add a UIButton to a UIToolbar just fine.Edit:
Sorry, you wanted a plain UIBarButtonItem. I don't believe you can do this with a UINavigationBar. You could try adding a UserInteractionEnabled UILabel as the rightBarButtonItem, I'm not sure if it will work or not.
Seems this isn't currently possible.sbwoodside has a solution.
为什么不试试 UI7Kit?对我来说效果很好,易于使用。
Why not try UI7Kit? Works well for me, easy to use.