我正在向导航栏添加图像,它工作正常,但是当我想添加标题时
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/