How can I have a UIBarButtonItem with both image and text?

前端 未结 6 1207
[愿得一人]
[愿得一人] 2020-11-29 05:17

When I try to use an image for a UIBarButtonItem, the text isn\'t shown. Is there a way to show both the text and the image?

6条回答
  •  醉梦人生
    2020-11-29 05:33

    I suggest a way how to do this using Storyboard:

    1. Drag and drop to ToolBar usual UIView. It automatically will be wrapped to Bar Button Item. Adjust View width in "Size inspector". Change background color of View to clear color.

    1. Put UIButton and UILabel inside a View. You can use constraints to adjust their positions. Also you can put thier outside of a View, for example, little higher or less (like on my picture). Put Default and Highlighted images for UIButton.

    2. Copy Bar Button Item and adjust another buttons. Add Flexible spaces.

    3. Create outlets.

    4. Done :). When you run app, you get Tool Bar like this:

    P.S. Here you can read how to create UIToolbar subclass using .xib

提交回复
热议问题