1

Below is the screenshot of the tab bar icon when the app is running. tabbar barbutton image

The icon says 27 which represents the day, and is an image.

Is there a way by which we can dynamically change the date according to current day?

Does the iPhone SDK has a way to change the tab bar bar button image dynamically according to the current date?

Ganesh Nayak
  • 802
  • 11
  • 38

1 Answers1

2

You have to change it yourself, but it is quite easy to get todays date.

And then from that date geting the day of the month is quite simple, here is how you can do it:

- (UIImage*)todaysImage{
    //Get todays date
    NSDate *today = [NSDate date];

    //Get the number using NSDateComponents
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:today];
    NSInteger day = [components day];

    //Load the apropiate image based on the number (this means you have an image for all 31 posible days)
    //This line also asumes your images are named DayImage-1.png, DayImage-2.png, DayImage-3.png, etc...
    UIImage *todaysDateImage = [UIImage imageNamed:[NSString stringWithFormat:@"DayImage-%d.png",day]];

    return todaysDateImage;
}

Then to set the image on yhou tab bar you would just call:

tabItem.image = [self todaysImage];

You could also generate your own images on the fly (and caching them so you dont have to generate every time). If you are interested in something like that, have a look at this:

How to capture UIView to UIImage without loss of quality on retina display

It will show you how to render a UIView into a UIImage object to use instead of having to pre-load all 31 images into the app.

Community
  • 1
  • 1
Zebs
  • 5,378
  • 2
  • 35
  • 49