0

我使用以下代码使用 UIView 自定义导航栏。但是为什么导航栏的左侧大小有一个缺口呢?附言。渐变图像是 test.png。

提前致谢!

在此处输入图像描述

- (void)viewDidLoad
{
[super viewDidLoad];

// show image
UIImage *image = [UIImage imageNamed: @"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);

// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = @"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];

CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}

编辑 1

这是我的测试代码和屏幕截图

在此处输入图像描述

- (void)viewDidLoad
{    

[super viewDidLoad];

UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;

[self.view addSubview:viewGreen];
}
4

2 回答 2

2

您的代码工作正常。只需检查图像和透明度。

最后添加该行并再次检查

[self.view addSubview:newView];
于 2012-08-24T10:58:47.493 回答
1

问题通过以下代码解决

[self.navigationBar insertSubview:imageView atIndex:1];
于 2012-09-16T08:15:06.050 回答