简体   繁体   中英

UILabel Background Image Disappears on iPhone 5 and below

I'm building a calendar app and have a series of UILabels displaying the dates. For any dates that have events, I put a small dot underneath the date by changing the labels background image with the following code:

label.backgroundColor = [UIColor colorWithPatternImage:newImage];

This works fine on iPhone 5s and above, but the images won't display on iPhone 5 or any earlier iPhones.

Could it be an issue with the image sizes I have in Images.xcassets?

I currently have 1x at 40x40, 2x at 80x80, and 3x and 120x120.

You can't add a background image to a UILabel, but you can add a UIImageView as a subview to a UILabel.

UIImageView *labelBackground = [[UIImageView alloc] 
initWithImage:[UIImage imageNamed:@"mybg.png"]];
[myLabel addSubview:labelBackground];

I found out it had nothing to do with the image sizes. After changing from using colorWithPatternImage to adding a Subview as suggested by @a4arpan, I noticed that the event information (date, time, etc) for dates were also not showing up in the table view underneath the calendar on iPhone 4s's and 5's.

To determine if I should display the dot for dates with events, I was checking if statements, ie:

if ([label.text == dateOfEvent]...)

I missed the fact that these are Strings and it should read as:

if ([label.text isEqualToString: dateOfEvent]

Once I made the change, it started working on all the available iPhones through the simulator. It's very strange that it was never an issue on the iPhone 5s, 6, etc. simulators, but it became an issue on the 4s and 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM