私は iOS 6 アプリを作成しており、特に指定しない限り、すべてのビューに表示されるタイトル ビューを設定したいと考えています。[[UINavigationBar appearance] setTitleView:view]
とを試しまし[[UIBarButtonItem appearance] setTitleView:view]
たが、どちらも機能しませんでした。
注: このタイトル ビューはラベルにすることはできません。ボタンにする必要があります。
何か案は?
私は iOS 6 アプリを作成しており、特に指定しない限り、すべてのビューに表示されるタイトル ビューを設定したいと考えています。[[UINavigationBar appearance] setTitleView:view]
とを試しまし[[UIBarButtonItem appearance] setTitleView:view]
たが、どちらも機能しませんでした。
注: このタイトル ビューはラベルにすることはできません。ボタンにする必要があります。
何か案は?
UINavigationController を使用していますか?
その場合は、次のようなものを使用して背景画像とフォントを設定します。
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:22.0], UITextAttributeFont,nil]];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar.png"] forBarMetrics:UIBarMetricsDefault];
UIViewController の特定のサブクラスでのみナビゲーション バーに変更を適用する場合は、次のようなものを使用します。
[[UINavigationBar appearanceWhenContainedIn:[VCSubclassToChangeAppearance class], nil]
setBackgroundImage:[UIImage imageNamed:@"navBar.png"]
forBarMetrics:UIBarMetricsDefault];
この方法を使用して、ルールの例外を作成することもできます。たとえば、特定の 1 つを除いて、すべてに対して 1 つの外観を設定します。
ナビゲーション バーの標準ボタンの外観を変更する場合は、次のようにします。
UIImage *standardBackgroundImage = [[UIImage imageNamed:@"navBarButton.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 8, 0, 8)];
[[UIBarButtonItem appearance] setBackgroundImage:standardBackgroundImage
forState:UIControlStateNormal
style:UIBarButtonItemStyleBordered
barMetrics:UIBarMetricsDefault];
この例では、すべてのナビゲーション バー ボタン (戻るボタンではない) であり、UIBarButtonItemStyleBordered の種類であり、画像を使用します。特定のボタン イメージの UIEdgeInsets で作業する必要があります。
異なるビュー コントローラーの UIBarButtonItem は、ナビゲーション バーのタイトル ビューを変更するため、タイトル ビューをグローバルに設定することはできません。UIViewController
メソッドをオーバーライドできる@Jake アプローチまたはサブクラスを使用できますsetTitle
。そして、そのクラスを独自のすべてのビュー コントローラーのスーパークラスとして使用します。
ナビゲーション バーにサブビュー (UITextField) を追加してみてください。
UITextField *titleField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100.0, 60.0)];
titleField.text = @"a title";
titleField.center = self.navigationController.navigationBar.center;
[self.navigationController.navigationBar addSubview:titleField];