Gibraltar Gibraltar - 4 years ago 130
iOS Question

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.

Answer Source

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, i.e.:

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download