iPhone SDK: Can a UIButton with custom images display the title?
If I create a custom UIButton with images for UIControlState normal and UIControlStateSelected, am I still supposed to be able to display the title for the button? Currently I can't. This is how I create my button:
UIImage *moveButtonImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"button_move_inactive" ofType:@"png"] ];
UIImage *moveButtonSelectedImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"button_move_pressed" ofType:@"png"] ];
self.moveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.moveButton setImage : moveButtonImage forState : UIControlStateNormal];
[self.moveButton setImage : moveButtonSelectedImage forState : UIControlStateSelected];
[self.moveButton setTitle:@"Test" forState:UIControlStateNormal];
The button title never displays in this case and I'm forced to create my own label to display on the button, which seems like a hacky way of doing things. Maybe I'm doing something 开发者_运维技巧wrong here?
There are two places images can be used in a UIButton, as the button itself, like you're doing:
setImage:forState:
or as the button's background:
setBackgroundImage:forState:
If you use setImage
then the image overrides the entire appearance of the button. If you instead use setBackgroundImage
then the button title remains visible.
精彩评论