To make things easier, I created a category in UIBarButtonItem that looks like this:
@implementation UIBarButtonItem (CustomButtonView) - (void)setButtonImage:(UIImage *)image { UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:image forState:UIControlStateNormal]; [button sizeToFit]; [button addTarget:self.target action:self.action forControlEvents:UIControlEventTouchUpInside]; self.customView = button; } - (UIImage *)buttonImage { return [(UIButton *)self.customView imageForState:UIControlStateNormal]; } @end
In client code, simply use:
myBarButtonItem.buttonImage = [UIImage imagedNamed:@"image_name"];
Made in this way, you can still connect your goals and actions to IB (clicking on the UI configuration in IB is a good thing, as you can).
Tylerc230 Jun 08 2018-12-12T00: 00Z
source share