0

次のコードを使用して、UIViewでナビゲーションバーをカスタマイズします。しかし、なぜナビゲーションバーの左側のサイズにギャップがあるのでしょうか。ps。グラデーション画像は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 に答える