【问题标题】:Gap is existing when customize the navigationbar with imageView使用 imageView 自定义导航栏时 Gap 令人兴奋
【发布时间】:2012-08-24 10:18:07
【问题描述】:

我使用以下代码来自定义带有 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];
}

【问题讨论】:

  • @Abizern Just CGRectMake(0, 0, 320, 44),也就是导航栏的框架
  • @Abizern 请检查我的问题的更新:)

标签: ios uiimageview uinavigationbar customization


【解决方案1】:

您的代码运行良好。只需检查图像和透明度即可。

最后添加一行并再次检查

[self.view addSubview:newView];

【讨论】:

    【解决方案2】:

    问题通过以下代码解决

    [self.navigationBar insertSubview:imageView atIndex:1];
    

    【讨论】:

      猜你喜欢
      • 2013-09-03
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多