【问题标题】:Custom UINavigationBar Image自定义 UINavigationBar 图片
【发布时间】:2013-01-02 10:08:37
【问题描述】:

我正在尝试为 iOS 6 实现自定义 UINavigationBar 图像背景,但我没有运气。它所显示的只是 Apple 的普通款(没有后退按钮)。 我在 viewDidLoad 中尝试了以下代码(一次一个):

[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];

[self.navigationController.navigationBar setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];

我通过以下方式呈现所需的视图:

optionViewController *choose= [[optionViewController alloc] initWithNibName:@"optionViewController" bundle:nil];
UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:choose];
[self presentViewController: aNavController animated:YES completion:nil];

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 发生了什么? [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barImage.png"] forBarMetrics:UIBarStyleDefault];此命令适用于我的代码中的任何地方,适用于 iOS5 和 iOS6。
  • @LêQuýSang 什么都没有发生...它只是显示典型的基本 UINaviationBar 而不是我想要的图像。谢谢

标签: objective-c ios


【解决方案1】:

您可以像这样设置导航栏自定义图像:-

        optionViewController *choose= [[optionViewController alloc] initWithNibName:@"optionViewController" bundle:nil];
        UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:choose];
        UIImage *image = [UIImage imageNamed:@"imageName"];
        [aNavController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

        [self presentModalViewController:aNavController animated:YES]; //change here to PresentViewcontroller to presentmodelViewcontroller

这里是图片:-

在上图中,Back 按钮不是默认按钮,而是以编程方式完成的。

【讨论】:

  • 感谢您的回复,但如果您看上面的内容,我已经尝试过了。还有其他建议吗?谢谢
  • 你尝试过,但你没有尝试使用这一行 UIImage *image = [UIImage imageNamed:@"navBarLoc.png"];试试这个
  • 啊抱歉,我认为这已经被理解了……我的实际项目中有那行代码。谢谢
  • 立即检查并实施
【解决方案2】:

试试这个,

if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    //iOS 5 new UINavigationBar custom background
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault];
} else {
    [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0];
}

有关信息,请转到此link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2010-10-16
    • 2012-04-23
    相关资源
    最近更新 更多