1

下面是应用程序运行时标签栏图标的屏幕截图。 选项卡栏按钮图像

图标上写着 27,它代表一天,是一个图像。

有没有办法可以根据当天动态更改日期?

iPhone SDK是否有办法根据当前日期动态更改标签栏栏按钮图像?

4

1 回答 1

2

您必须自己更改它,但很容易获得今天的日期。

然后从那个日期开始获取月份的日期非常简单,您可以这样做:

- (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;
}

然后在你的标签栏上设置图像,你只需调用:

tabItem.image = [self todaysImage];

您还可以即时生成自己的图像(并缓存它们,这样您就不必每次都生成)。如果你对类似的东西感兴趣,看看这个:

如何在不损失视网膜显示质量的情况下将 UIView 捕获到 UIImage

它将向您展示如何将 UIView 渲染到 UIImage 对象中使用,而不必将所有 31 张图像预加载到应用程序中。

于 2012-08-22T04:19:22.617 回答