31

ナビゲーション バー全体を占める画像が必要です。これは、ナビゲーションベースのアプリに付属のナビゲーションです。これは、付随する UITableView と共に RootViewController に表示されます。これがどのように機能するかの例をいくつか見てきました。

ナビゲーション バーのタイトルを設定します。

UIImage *image = [UIImage imageNamed:@"TableviewCellLightBlue.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar.topItem setTitleView:imageView];

問題は、ナビゲーション バー全体ではなく、タイトルのみをカバーしていることです。

このスレッドもあります: http://discussions.apple.com/message.jspa?messageID=9254241#9254241。終わりに向かって、ソリューションはタブバーを使用しているように見えますが、私は使用していません。ナビゲーションバーの背景を設定するのはそんなに複雑ですか? 他の簡単なテクニックはありますか?

ナビゲーションの背景を持ち、タイトル テキストを使用できるようにしたいと考えています。

4

9 に答える 9

36

あなたの場合、別の答えで見つかったこの解決策はうまくいくでしょう。

UINavigationBarに「CustomImage」カテゴリを追加すると、次のように呼び出すことができます。

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"yourNavBarBackground.png"];
[navBar setBackgroundImage:image];

このコードはメソッドに含める必要があります

- (void)viewWillAppear:(BOOL)animated

カスタムイメージが必要なViewControllerの そして、その場合、あなたはより良い電話をするべきです:

[navBar clearBackgroundImage]; // Clear any previously added background image

setBackgroundImageの前(それ以外の場合は複数回追加されます...)

于 2009-10-12T08:27:55.187 に答える
23

iOS 6 用に変更され、iOS 6 で動作するようにするには、次のようにします。

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
于 2012-09-27T08:38:00.370 に答える
9
UIImage *image = [UIImage imageNamed:@"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

[self.navigationController.navigationBar addSubview:imageView];
于 2011-01-11T05:47:51.937 に答える
8

実際には、任意のUIViewクラスまたはサブクラスに背景画像を追加するはるかに簡単な方法があります。クラスの分類や拡張 (サブクラス化) は必要なく、「必要に応じて」これを行うことができます。たとえば、View Controller のナビゲーション バーに背景画像を追加するには、次のようにします。

self.navigationController.navigationBar.layer.contents = (id)[UIImage 
    imageNamed:@"background.png"].CGImage;

Quartz Core フレームワークをプロジェクトに追加#import <QuartzCore/QuartzCore.h>し、これを行う必要がある場所に追加することを忘れないでください。これは、UIView. もちろん、すべてのナビゲーション バーまたはタブ バーに対して同様の効果を達成したい場合は、サブクラス化が理にかなっています。

于 2010-11-02T15:15:49.250 に答える
4

この行を追加するだけです。

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];

バム!1行で完了です。

于 2013-06-26T16:59:19.037 に答える
4
UIImage *logo = [UIImage imageNamed:@"my_logo"];
UIImageView *logoView = [[UIImageView alloc]initWithImage:logo];
logoView.frame = CGRectMake(0, 0, 320, 37);

UINavigationController *searchNavCtrl = [[UINavigationController alloc] initWithRootViewController:searchViewController];
searchNavCtrl.navigationBar.barStyle = UIBarStyleBlack;


//searchNavCtrl.navigationItem.titleView = logoView;
//[searchNavCtrl.navigationController.navigationBar.topItem setTitleView:logoView];
[searchNavCtrl.navigationBar addSubview:logoView];

[logoView release];
于 2012-05-04T21:25:05.747 に答える
3

cmpのソリューションを使用し、それを削除するためのロジックを追加しました。これは、ホーム画面のカスタム背景画像のみが表示されるようにするためです。

HomeViewController.m

UIImage *image = [UIImage imageNamed:@"HomeTitleBG.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tag = 10;

UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];

if ( testImgView != nil )
{
    NSLog(@"%s yes there is a bg image so remove it then add it so it doesn't double it", __FUNCTION__);
    [testImgView removeFromSuperview];

} else {
    NSLog(@"%s no there isn't a bg image so add it ", __FUNCTION__);
}

[self.navigationController.navigationBar addSubview:imageView];


[imageView release];

また、提案されたclearBackgroundImageメソッドを使用しようとしましたが、機能させることができなかったため、画像にタグを付けてから、他のビューコントローラーで削除すると表示されます。

OtherViewController.m

UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];

if ( testImgView != nil )
{
    NSLog(@"%s yes there is a bg image so remove it", __FUNCTION__);
    [testImgView removeFromSuperview];  
} 

`

于 2011-04-30T15:34:48.140 に答える
0

appdelegate にコードを追加すると、メソッドの起動が完了しました

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
else
{
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    // Uncomment to assign a custom backgroung image
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];

    // Uncomment to change the back indicator image
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

    [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]];

    // Uncomment to change the font style of the title

    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    shadow.shadowOffset = CGSizeMake(0, 0);

    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"HelveticaNeue-Bold" size:17], NSFontAttributeName, nil]];

    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
于 2015-09-14T09:36:27.850 に答える