iOS5.0以降では、tintColorを利用できるクラスが増えました。iOS4系以前との互換性を保つためには、新規導入されたtintColorを使わないか、tintColorが実装されていない場合の代替処理を記述しておく必要があるので注意が必要です。
iOS 5.0以降
以下の3つのクラスではiOS5.0以降でtintColorを設定できます。
UIBarButtonItem
UIButton
UITabBar
iOS2.0以降
以下の4つのクラスではiOS2.0以降でtintColorを設定できます。
UINavigationBar
UISearchBar
UISegmentedControl
UIToolbar
UIScrollViewを使って、横方向にページを並べてみます。Empty Applicationを作成し、ApplicationDelegate.mの起動メソッドを以下のように書き換えます。PAGE_COUNTにはページ数を設定しておいてください。(例: #define PAGE_COUNT 3)
- (BOOL)application
:(UIApplication
*)application didFinishLaunchingWithOptions
:(NSDictionary *)launchOptions
{
self.window
= [[UIWindow alloc
] initWithFrame
:[[UIScreen mainScreen
] bounds
]];
UIScrollView
* scrollView
= [[UIScrollView alloc
] init
];
scrollView.backgroundColor
= [UIColor whiteColor
];
//ページ数分の横幅を確保します
scrollView.contentSize
= CGSizeMake
(320.0 * PAGE_COUNT, 400.0f
);
//1ページごとにめくれるように設定。これがないと中途半端な位置で止まります。
scrollView.pagingEnabled
= YES;
for(int i
= 0; i < PAGE_COUNT; i
++){//ページの中身を作成
UILabel
* label
= [[UILabel alloc
] initWithFrame
:CGRectMake
(320.0f
* i,
0,
100,
100)];
label.text
= [NSString stringWithFormat
:@"%d", i
];
label.font
= [UIFont fontWithName
:label.font.fontName size
:40.0f
];
label.textAlignment
= UITextAlignmentCenter;
label.backgroundColor
= [UIColor yellowColor
];
[scrollView addSubview
:label
];
}
//ナビゲーションコントロールの中に設定してみます。
UIViewController
* scrollViewController
= [[UIViewController alloc
] init
];
scrollViewController.view
= scrollView;
UINavigationController
*navigationController
= [[UINavigationController alloc
] initWithRootViewController
:scrollViewController
];
self.window.rootViewController
= navigationController;
[self.window makeKeyAndVisible
];
return YES;
}
スクリーンショット

ナビゲーションバーの表示を切り替える
スクロールを検知してナビゲーションバーの表示を切り替えます。以下の二点だけを書き換えると、プロトコルをサポートしていないと警告でるので、適宜変更して下さい。また、scrollViewControllerは複数メソッドから利用するので、AppDelegateのプロパティに移動しましょう。よくわからない場合はプロジェクトをダウンロードしてご確認下さい。
//以下の行を起動メソッドに追加
scrollView.delegate
= self;
//ApplicationDelegate.mに以下のメソッドを追加
-(void)scrollViewDidScroll
:(UIScrollView
*)_scrollView
{
scrollViewController.title
= [NSString stringWithFormat
:@"%d",
(int)round
(_scrollView.contentOffset.x
/ 320.0f
) + 1];
}
スクリーンショット

プロジェクトをダウンロード: HorizontalScroll
iPhoneアプリ開発に関する情報を公開中 ソースコードもあるよ。