UIToolbar UIBarButtonItem with both image and title has very dim text

前端 未结 3 896
情话喂你
情话喂你 2020-12-10 08:28

My iPhone view adds some custom buttons to its toolbar. Each button has both an image and textual title, and is created like this:

UIBarButtonItem *fooButton         


        
3条回答
  •  自闭症患者
    2020-12-10 08:58

    Johnus's solution is pretty useful. But I've tried it and a little problem occured: the action was not sent to the bar button item, so I've modified the initializer a little :

    - (id) initWithImage:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)action {
    
        UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIFont *font = [UIFont boldSystemFontOfSize:13];
        barButton.titleLabel.font = font;
        barButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
        barButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 5);
        [barButton setImage:image forState:UIControlStateNormal];
        [barButton setTitle:title forState:UIControlStateNormal];
        [barButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [barButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [barButton setTitleShadowColor:[[UIColor blackColor] colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
        [barButton setBackgroundImage:[UIImage imageNamed:@"bar-button-item-background.png"] forState:UIControlStateNormal];
        barButton.frame = CGRectMake(0, 0, image.size.width + 15 + [title sizeWithFont:font].width, 30);
    
        if (self = [super initWithCustomView:barButton]) {
            self.target = target;
            self.action = action;
            // I've added just one line of code here
            [barButton addTarget:target
                          action:action
                forControlEvents:UIControlEventTouchUpInside];
        }
    
        return self;
    }
    

提交回复
热议问题