UIButton with background image: title text randomly does not appear
I am setting the background image on a UIButton which has title text. This works fine, except that about 5% of the time, rarely and non-deterministically, with the same code, the text does not appear.
In my UIButton subclass:
[self setBackgroundImage:normalImage forState:UIControlStateNormal];
// Also set a background image for all other states.
In a UIButton category, I have this convenience method for setting the title:
- (void)setNormalTitle:(NSString *)title {
[self setTitle:title forState:UIControlStateNormal];
[self setTitle:title forState:UIControlStateSelected];
[self setTitle:title forState:UIControlStateHighlighted];
[self setTitle:title forState:UIControlStateSelected | UIControlStateHighlighted];
}
I'm not finding this issue having been encountered by anyone. Any ideas? I can always create a custom UIView which contains the button and draws text on top using a UILabel, but I wonder if there's a known issue I'm encountering here.
Update : I seem to have worked around the issue by setting the title asynchronously:
_tabCell = loadTopNibObject(@"MYTabSmall3HeaderCell");
dispatch_async(dispatch_get_main_queue(), ^{
_tabCell.tab0Button.normalTitle = @"Option 1";
_tabCell.tab1Button.normalTitle = @"Option 2";
_tabCell.tab2Button.normalTitle = @"Option 3";
});
Update 2 : Nope, the above doesn't work. I've breakpointed and printed out the empty button's text, which reports the correct string even though the button appears blank. I've also tried running setNeedsDisplay:
when the button is touched, to no avail. Also tried re-setting the button text to some specific string (eg @"test"
) on touch but this doesn't kick in any visible letting.
上一篇: iOS AutoLayout多