1

私は iOS 6 アプリを作成しており、特に指定しない限り、すべてのビューに表示されるタイトル ビューを設定したいと考えています。[[UINavigationBar appearance] setTitleView:view]とを試しまし[[UIBarButtonItem appearance] setTitleView:view]たが、どちらも機能しませんでした。

注: このタイトル ビューはラベルにすることはできません。ボタンにする必要があります。

何か案は?

4

5 に答える 5

1

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 で作業する必要があります。

于 2013-07-11T02:10:04.283 に答える
0

異なるビュー コントローラーの UIBarButtonItem は、ナビゲーション バーのタイトル ビューを変更するため、タイトル ビューをグローバルに設定することはできません。UIViewControllerメソッドをオーバーライドできる@Jake アプローチまたはサブクラスを使用できますsetTitle。そして、そのクラスを独自のすべてのビュー コントローラーのスーパークラスとして使用します。

于 2013-07-11T02:15:59.753 に答える
0

ナビゲーション バーにサブビュー (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];
于 2013-07-11T01:52:52.247 に答える