Button image not displayed although others are in its place
i have a .png image that i am trying to display as a UIBarButtonItem . Here is the sintax :
_gotoButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_info.png"]
style:UIBarButtonItemStylePlain
target:self action:@selector(gotoAction)];
The thing is that when i try to display this image, in its place, there's a white rectangular the size of the photo.
The odd thing is that when i try to load 开发者_C百科other images, it works.
My first thought was that the format is incorrect, but then i used an on-line convertor and it still doesn't work. What could be the problem?
Images on UIBarButtonItem works only if they are of single color (similar to tab bar images).
Other option could be use UIBarButtonItemStyleCustom and set an UIImageview as buttons' custom view.
You might need to create a custom button with image on it and use the initWithCustomView: method of barbuttonitem..
UIButton *bButton = [UIButton buttonWithType:UIButtonTypeCustom];
[bButton setImage:[UIImage imageNamed:@"btn_info.png"] forState:UIControlStateNormal];
bButton.frame = CGRectMake(278,7, 32, 30);
[bButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *questionButton = [[UIBarButtonItem alloc]
initWithCustomView:bButton];
[questionButton setStyle:UIBarButtonItemStylePlain];
- Check the spelling of the file name in the
imageNamed:
method. - Open the image in an external viewer, such as Preview and check that it looks the way you want. Check for alpha channels in particular.
Make sure you've added the image file to your Xcode project as well.
精彩评论