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];

オンザフライで独自の画像を生成することもできます (毎回生成する必要がないようにキャッシュします)。もしあなたがそのようなことに興味があるなら、これを見てください:

Retinaディスプレイで品質を落とさずにUIViewをUIImageにキャプチャする方法

31 個の画像すべてをアプリにプリロードする代わりに、UIView を UIImage オブジェクトにレンダリングして使用する方法を示します。

于 2012-08-22T04:19:22.617 に答える