AnastasiaY AnastasiaY - 6 months ago 54
Objective-C Question

UIBarButtonItems invisible issue

I have a toolbar added to the view in interface builder. And I need to add some buttons to it programmatically. So what I do is calling this method inside the viewDidLoad method. The items are added as the action method is executed but they are not visible.

- (void) addingNumberOfLettersBar {
UIImage *image1 = [UIImage imageNamed:@"1"];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:image1 forState:UIControlStateNormal];
//UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithImage:image1 style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
//UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:@selector(changeNumberOfLettersToShow:)];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
barButtonItem1.tag = 1;

UIImage *image3 = [UIImage imageNamed:@"3"];
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithImage:image3 style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
barButtonItem3.tag = 3;

UIImage *image4 = [UIImage imageNamed:@"4"];
UIBarButtonItem *barButtonItem4 = [[UIBarButtonItem alloc] initWithImage:image4 style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
barButtonItem4.tag = 4;

UIImage *image5 = [UIImage imageNamed:@"5"];
UIBarButtonItem *barButtonItem5 = [[UIBarButtonItem alloc] initWithImage:image5 style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
barButtonItem5.tag = 5;

UIImage *image5p = [UIImage imageNamed:@"5+"];
UIBarButtonItem *barButtonItem5p = [[UIBarButtonItem alloc] initWithImage:image5p style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
barButtonItem5p.tag = 0;

UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

NSArray *toolbarItems = [NSArray arrayWithObjects:barButtonItem1, flexibleItem, barButtonItem3, flexibleItem, barButtonItem4, flexibleItem, barButtonItem5, flexibleItem, barButtonItem5p, nil];


self.toolbar.items = toolbarItems;
}

Answer

Check your image size. It should not be much large. Set tint color to every button and check is it visible or not.

and you should create barbuttonitem in your case like,

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick:)];