iPhone UIBarButtonItem setCustomView:方法?
我想自定义 UIBarButtonItem 的外观。这是我当前使用的代码:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(267, 6, 48, 30)];
[editButton setCustomView:button]; // editButton is the UIBarButtonItem
不幸的是,UIButton 没有被绘制,即它是 100% 透明的。 我知道按钮在那里,因为当我在代码中添加一行时,如下所示:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(267, 6, 48, 30)];
[editButton setCustomView:button]; // editButton is the UIBarButtonItem
UIRectFill(button.frame);
我看到一个黑色方块出现在按钮应该出现的位置。
我怎样才能绘制我的 UIButton?我已经看了很多地方,但似乎没有任何效果......
I want to customize my UIBarButtonItem's appearance. Here's the code I'm currently using:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(267, 6, 48, 30)];
[editButton setCustomView:button]; // editButton is the UIBarButtonItem
Unfortunately, the UIButton does not get drawn, i.e. it's 100% transparent.
I know the button is there because when I add a line to the code, like so:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(267, 6, 48, 30)];
[editButton setCustomView:button]; // editButton is the UIBarButtonItem
UIRectFill(button.frame);
I see a black square appearing in the position where the button should appear.
How can I get my UIButton to be drawn? I already looked at a ton of places, but nothing seems to work ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为你可以将一个按钮的自定义视图设置到另一个按钮。按钮是复杂的对象,像这样嵌套它们通常是行不通的。
自定义视图属性应该用于装饰,而不是功能。您应该加载具有您想要的 UIBarButtonItem 外观的自定义视图,并让它处理实际的按钮逻辑。
I don't think you can set the custom view of a button to another button. Buttons are complex objects and nesting them like that usually won't work.
The custom view attribute is supposed to be for decoration, not functionality. You should load a custom view with appearance you want for the UIBarButtonItem and let it handle the actual button logic.
您是否尝试使用 UIImageView 的自定义视图来初始化它?如果它有效,您可以以如下方式创建 UIImageView:它伪造经典 UIButton,如下所述:
如何使用 iPhone SDK 创建一个大的红色 UIButton?
但更一般地说,从 UI 角度来看,在“UIBarButtonItem”的位置使用经典的“UIButton”很奇怪:/
Did you try to init it with a Custom view that is a UIImageView ? If it works you could create the UIImageView in such a way that it fakes a classic UIButton as described here :
How can I create a big, red UIButton with the iPhone SDK?
But more generally, from a UI perspective, it's strange to have classic "UIButton" at the place of "UIBarButtonItem" :/
请参阅我对此问题的回答。
我认为最好避免将
UIButton
放入UIBarButtonItem
中。它们或多或少做同样的事情,并且 BBI 的存在是有原因的 - 与 UIButton 相比,它们是轻量级的,并且它们与 UINavigationItems 有着良好的舒适关系。我认为如果您让我们知道您想要实现的总体目标是什么,我们可以提供更好的帮助?这样我们就可以帮助提供可行的解决方案。
Consult my answer to this question.
I think it's best to avoid putting a
UIButton
inside aUIBarButtonItem
. They do the same thing more or less, and BBIs exist for a reason - they're lightweight compared to UIButtons, and they have a nice cozy relationship withUINavigationItems
.I think we can help a little better if you let us know - what is the overall purpose you're trying to achieve? That way we can help provide a viable solution.
我也遇到了类似的问题;该按钮没有出现,点击该空间也没有任何反应。设置按钮的范围似乎是让它发挥作用的魔力。
这是我用来设置右侧按钮的代码:
I was having a similar problem; the button didn't appear and tapping in that space did nothing, either. Setting the bounds of the button seemed to be the magic that makes it work.
Here's the code that I'm using to set the right button: