【问题标题】:How do I add a UIBarButtonItem to a UIToolbar with both text and an image in it?如何将 UIBarButtonItem 添加到包含文本和图像的 UIToolbar?
【发布时间】:2011-12-01 05:30:08
【问题描述】:

如何将UIBarButtonItem 添加到UIToolbar 中,其中包含文本和图像?看看我展示的示例图像。这正是我想要做的。

【问题讨论】:

    标签: iphone objective-c xcode cocoa-touch ipad


    【解决方案1】:

    首先创建一个带有图片和标题的 UIButton。

    然后将该按钮添加到 BarButtonItem

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
    

    【讨论】:

    • 是的,但这是否意味着我必须完全设置按钮的样式以使其看起来与普通 UIBarButton 完全一样?使用标准 UIBarButtonItem 的优点是可以在按钮中间获得光泽。
    • @Ethan Allen :为此,您需要将按钮图像设计为类似于 barbuttonItem。每个人都会这样做。
    【解决方案2】:

    向工具栏添加自定义按钮 将此属性设置为该按钮 在按钮上添加标签以显示文本

    button.contentHorizontalAlignment =  UIControlContentHorizontalAlignmentLeft;
    

    【讨论】:

      【解决方案3】:

      您应该使用UILabelUIImage 作为子视图创建UIView。然后用自定义视图创建UIBarButtonItem

      UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:customView];
      

      【讨论】:

        【解决方案4】:

        几个参考链接将指导您如何将 UIBarButtonItem 实现为带有文本和图像的 UIToolbar.... 您只需根据您的要求修改代码即可。

        1. UIToolbar UIBarButtonItem with both image and title has very dim text

        2. add image to UIBarButtonItem for UIToolbar

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-04
          • 1970-01-01
          • 2018-05-31
          • 2016-11-24
          • 2019-12-14
          相关资源
          最近更新 更多