如何在ios 5和ios 6中自定义导航栏?

问题描述:

正如我在这里搜索时看到的那样,在同一主题上有很多问题,但我真的不知道要使用哪些代码以及将它们放在哪里。如何在ios 5和ios 6中自定义导航栏?

我只是想将导航栏更改为具有自己的背景图像的自定义栏。

我看过一些代码,但我真的很新,所以我不知道该怎么做!

Like this

-Simon

+0

您的屏幕截图显示了一个标准导航栏。您可以使用文档中描述的外观方法自定义导航栏。你能解释你遇到麻烦的部分吗? – jrturton

+0

但我在哪里把代码?而我不知道客观的c好,努力吧! – SimonZ1

使用外观性

[[UINavigationBar的外观]了setBackgroundImage:yourimage forBarMetrics:UIBarMetricsDefault];

+0

我把代码放在哪里? – SimonZ1

+0

我已经搜遍遍了,但它只是一个旧代码和这样的东西混搭。我不知道该把它放在哪里! – SimonZ1

+0

如果要为所有类添加导航栏,请在应用程序委托中尝试它,或者将其添加到要添加导航栏的视图控制器中 –

//first put this code in AppDelegate.m 
    in this method: 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
     { 


     //code: navigationbar set image and fontcolor: 

      [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"your_navigationimg_bg"] forBarMetrics:UIBarMetricsDefault]; 

      [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault]; 

      [[UINavigationBar appearance] setTitleTextAttributes: @{ 
            UITextAttributeTextShadowColor: [UIColor clearColor], 
           UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], 
              UITextAttributeFont: [UIFont fontWithName:@"AppleGothic" size:20.0f] 
      }]; 



      [self.window makeKeyAndVisible]; 
      return YES; 
     } 


//second: put this code in RootViewController.m 
    in this method: 

- (void)viewDidLoad 
{ 

    [email protected]"your title"; 


    [super viewDidLoad]; 
}