使用背景图像自定义 UIBarbuttonitem
我添加了一个 UIToolbar 实例和其顶部的按钮。每个按钮都属于 UIBarButtonItem 类。
我的要求是每个按钮都有一个自定义的布局,我不想使用苹果提供的原生按钮样式。所以我在 Interface Builder 中有 3 个选项(普通、边框、完成)。 我选择了“平原”样式,并选择了要添加为背景的图像 栏目->图像。
但它对我来说不起作用,使用普通选项让我更接近一点,因为边框和完成距离更近,但它仍然以白色轮廓显示图像。无论如何,是否添加了按钮上的图像,并且它们看起来完全一样。使用 UIButton 时这是一个非常简单的任务。我只是在 UIButton 的“属性”检查器中使用“图像”选项,然后选择我想要的图像。但在这里使用 UIBarButtonitem 时它根本不起作用。 这就是它的显示方式
谢谢 泰穆尔
I have added an instance of UIToolbar and buttons on top of it . Each button's belongs to the class of UIBarButtonItem.
My Requirement is that each button has a customized layout , i dont want to use the native button styles provided by Apple. So i had 3 options in Interface Builder ( Plain , Bordered , Done ).
I have selected the style Plain and selected the image i want to add as backgroun under
Bar item -> Image.
But it didnt work for me, using the plain option gets me a little closer as border and done are nowhere closer but still it displays the images in white outline. Is there anyway that images on the buttons are added and they look exactly as they are. Its a pretty simple task while using UIButton. I just used Image option in the Attributes inspector for UIButton and select the image i want. But here while using UIBarButtonitem it doesnt work at all.
This is how it displays
Thanks
Taimur
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Taimur,
我遇到了和你遇到的同样的问题。我们的设计师为应用程序导航栏中的按钮指定了自定义外观。这是我编写的用于生成我们需要的 UIBarButtonItems 的实用方法,您应该能够根据您的需要修改它:
Taimur,
I ran into the same problem that you're hitting. Our designer had specified a custom look for the buttons in our app's navigation bar. Here is a utility method that I wrote to generate the UIBarButtonItems that we needed, you should be able to modify it to your needs:
这里的问题是, UIBarButtonItem 不是 UIView 子类。
我也遇到了同样的问题,解决方案非常简单:
使用自定义背景图像创建一个 UIButton 对象,然后使用其
initWithCustomView:
方法创建 UIBarButtonItem 的实例,并将 UIButton 对象作为参数传递。希望我能帮助你。
The problem here is, that UIBarButtonItem is not a UIView-Subclass.
I had the same problem too and the solution is quite trivial:
Create a UIButton Object with your custom background image, afterwards create an instance of a UIBarButtonItem using its
initWithCustomView:
method and passing your UIButton object as a parameter.Hope I could help you.
将一个 UIButton 拖放到 IB 中的 UIToolBar 之外,然后对其进行格式化并分配选择器并将其拖入 UIToolBar .,.,, 这对我有用!
Drag and Drop a UIButton out side the UIToolBar in IB then format it and assign the selectors and drag it into UIToolBar .,.,, This worked for me!!
我在导航栏中的 UIBarButton 遇到了同样的问题。
我使用了kaar3k提供的解决方案。谢谢卡尔3k。
将 UIButton 拖放到 Storybaord 中的导航栏之外,然后对其进行格式化并分配选择器并将其拖动到导航栏上!
I had the same problem with a UIBarButton in the Navigation Bar.
I used the solution offered by kaar3k. Thanks kaar3k.
Drag and Drop a UIButton out side the NavBar in Storybaord then format it and assign the selectors and drag it onto the NavBar!!