Reputation: 271
I have an iOS app with a UIToolbar with several buttons on it. One of them I change the image programatically based on the date as follows:
[_button setImage: [UIImage imageNamed: @"blah"]];
_button is an IBOutlet.
On the iPhone it works just fine but every time I run the app on the iPad I get the glitch shown in the image bellow. Any idea what might be causing this?
Upvotes: 1
Views: 429
Reputation: 137
Try This
UIButton *btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 20, 20)];
[btton addTarget:self action:@selector(actionMenu:) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:@“blah.png"] forState:UIControlStateNormal];
UIBarButtonItem * barbutton = [[UIBarButtonItem alloc] initWithCustomView:btton];
Upvotes: 1
Reputation: 2503
Let's try:
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setFrame: CGRectMake(0, 0, closeButtonFontSize.width, closeButtonFontSize.height)];
[closeButton.titleLabel setFont: font];
[closeButton setTitle: closeStr forState: UIControlStateNormal];
[closeButton setTitleColor: hoverColor forState: UIControlStateHighlighted];
[closeButton addTarget:self action:@selector(closePressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *closeBarButton = [[UIBarButtonItem alloc] initWithCustomView:closeButton];
//set this bar button to your toolbar
Upvotes: 2
Reputation: 4901
SetImage directly not working for button, we set image and state for button like this
[_button setImage:[UIImage imageNamed: @"blah"] forState:UIControlStateNormal];
UIBarButtonItem with image
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action)];
else you customize your barbuttonitem
Upvotes: 1