iphone - 在 iPhone 中设置 UI 导航栏的标题

标签 iphone ipad uinavigationbar title

我正在向导航栏添加图像,它工作正常,但是当我想添加标题时

   self.title=@"Activity";

它没有显示任何内容

使用另一种方式我也添加标题标签,但这对于一个 View Controller 来说效果很好,但在第二个 Controller 上我更改了标题,但它也显示了以前的标题

这是我的代码

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




[self.navigationController.navigationBar addSubview:imageView]; 


   titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(50,2,250,36)];
     titleLabel.text=@"Activity";
titleLabel.textColor=[UIColor whiteColor];
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size :18];



//titleLabel boldSystemFontOfSize:14.0;
[self.navigationController.navigationBar addSubview:titleLabel];

最佳答案

不要在 UINavigationBar 中添加图像,而是使用以下代码设置背景图像..

UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:@"Nav.png"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

然后像下面一样设置标题...

self.title=@"Activity";

更新

    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
    {
        [navBar setBackgroundImage:[UIImage imageNamed:@"Nav.png"] forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        UIImageView *imageView = (UIImageView *)[navBar viewWithTag:1];//any tag
        if (imageView == nil)
        {
            imageView = [[UIImageView alloc] initWithImage:
                        [UIImage imageNamed:@"Nav.png"]];
            [navBar insertSubview:imageView atIndex:0];
            [imageView release];
        }
    }
    self.title=@"Activity";

关于iphone - 在 iPhone 中设置 UI 导航栏的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17341575/

相关文章:

ios - iPhone 5 分辨率错误

iphone - 如何让xCode项目中的启动画面持续时间更长?

iphone - Objective-C:如何获取 `objc_property_t` 属性的值?

ios - NavigationBar barTintColor 显示相同颜色的不同效果

objective-c - 导航按钮点击外部

iphone - 使用 XMLVM 将 android 应用程序转换为 iphone 应用程序

iphone - OpenGL ES 2.0 和顶点缓冲区对象 (VBO)

iphone - 将数组复制到另一个数组中

ios - Safari iOS | 100%-高度页面对于视口(viewport)来说太高了

ios - 在self中添加导航栏