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

前端 未结 6 1132
[愿得一人]
[愿得一人] 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:29

    Swift Update for UIBarButtonItem with a custom view that has both image and text.

    var chatImage: UIImage  = UIImage(named: "YourImageName")
    var rightButton : UIButton = UIButton(type: UIButtonType.custom)
    rightButton.setBackgroundImage(rightImage, for: .normal)
    rightButton.setTitle(title, for: .normal)
    rightButton.frame.size = CGSize(width: 100, height: 30)
    
    let menuUIBarButtonItem = UIBarButtonItem(customView: rightButton)
    
    0 讨论(0)
  • 2020-11-29 05:31

    Kris Markel's answer is a good start (only < iOS7), but if you use tint color, it wount work for the image and the highlighted state of the button.

    I've created a category that will create a UIBarButtonItem (leftBarButtonItem) that looks and behaves like a normal iOS7 back button. Have a look at: https://github.com/Zero3nna/UIBarButtonItem-DefaultBackButton

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-29 05:40

    Swift 4.2 with target

    extension UIBarButtonItem {
        convenience init(image :UIImage, title :String, target: Any?, action: Selector?) {
            let button = UIButton(type: .custom)
            button.setImage(image, for: .normal)
            button.setTitle(title, for: .normal)
            button.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
    
            if let target = target, let action = action {
                button.addTarget(target, action: action, for: .touchUpInside)
            }
    
            self.init(customView: button)
        }
    }
    
    0 讨论(0)
  • 2020-11-29 05:49
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
    [button addTarget:target action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 53, 31)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
    [label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:13]];
    [label setText:title];
    label.textAlignment = UITextAlignmentCenter;
    [label setTextColor:[UIColor whiteColor]];
    [label setBackgroundColor:[UIColor clearColor]];
    [button addSubview:label];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barButton;
    
    0 讨论(0)
  • 2020-11-29 05:51

    You can init the UIBarButtonItem with a custom view that has both image and text. Here's a sample that uses a UIButton.

    UIImage *chatImage = [UIImage imageNamed:@"08-chat.png"];
    
    UIButton *chatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [chatButton setBackgroundImage:chatImage forState:UIControlStateNormal];
    [chatButton setTitle:@"Chat" forState:UIControlStateNormal];
    chatButton.frame = (CGRect) {
        .size.width = 100,
        .size.height = 30,
    };
    
    UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:chatButton] autorelease];
    self.toolbar.items = [NSArray arrayWithObject:barButton];
    
    0 讨论(0)
提交回复
热议问题