Setting title for UINavigation bar in iphone

前端 未结 3 1284
滥情空心
滥情空心 2021-01-22 05:09

I am adding image to navigation bar it works fine but when i want to add title

   self.title=@\"Activity\";

it does not show anything

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 05:54

    Instead of add Image in UINavigationBar , set background Image using bellow code..

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

    and then set Title like bellow...

    self.title=@"Activity";
    

    UPDATE

        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";
    

提交回复
热议问题