iPhone 中的 UI 导航栏背景

发布于 2024-08-03 10:51:36 字数 1384 浏览 4 评论 0原文

我已将以下代码应用到我的应用程序中以更改导航栏图像。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    [self setNavigationBarTitle];
}

-(void)setNavigationBarTitle {
    UIView *aViewForTitle=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease];
    UIImageView *aImg=[[UIImageView alloc] initWithFrame:CGRectMake(-8, 0, 320, 45)];
    aImg.image=[UIImage imageNamed:@"MyTabBG.png"];
    [aViewForTitle addSubview:aImg]; [aImg release]; 
    UILabel *lbl=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 305, 45)] autorelease];
    lbl.backgroundColor=[UIColor clearColor]; lbl.font=[UIFont fontWithName:@"Trebuchet MS" size:22];
    lbl.shadowColor=[UIColor blackColor]; [lbl setShadowOffset:CGSizeMake(1,1)];
    lbl.textAlignment=UITextAlignmentCenter; lbl.textColor=[UIColor whiteColor]; lbl.text=@"Mobile Tennis Coach Overview";
    [aViewForTitle addSubview:lbl];
    [self.navigationItem.titleView addSubview:aViewForTitle];
}

请参阅以下图片。你可以看到我面临的问题。

替代文本


替代文本

我的应用程序的每个视图控制器都有上述方法来设置导航栏背景。

但是,当我将新的视图控制器推送到我的应用程序时。将出现后退按钮。

我需要出现后退按钮。但图像应该位于后退按钮后面。

I have applied following code to my application to change the navigation bar image.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    [self setNavigationBarTitle];
}

-(void)setNavigationBarTitle {
    UIView *aViewForTitle=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease];
    UIImageView *aImg=[[UIImageView alloc] initWithFrame:CGRectMake(-8, 0, 320, 45)];
    aImg.image=[UIImage imageNamed:@"MyTabBG.png"];
    [aViewForTitle addSubview:aImg]; [aImg release]; 
    UILabel *lbl=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 305, 45)] autorelease];
    lbl.backgroundColor=[UIColor clearColor]; lbl.font=[UIFont fontWithName:@"Trebuchet MS" size:22];
    lbl.shadowColor=[UIColor blackColor]; [lbl setShadowOffset:CGSizeMake(1,1)];
    lbl.textAlignment=UITextAlignmentCenter; lbl.textColor=[UIColor whiteColor]; lbl.text=@"Mobile Tennis Coach Overview";
    [aViewForTitle addSubview:lbl];
    [self.navigationItem.titleView addSubview:aViewForTitle];
}

See the following images. You can see the problem that I am facing.

alt text


alt text

Each view controller of my application has above methods to set the navigation bar background.

However, when I push a new view controller to my application. Back button will be appear.

I need back button to be appear. But the image should be behind back button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野味少女 2024-08-10 10:51:36

经过一个烦人的夜晚后,如果您使用drawLayer,我发现对此有一个轻微的调整。使用drawRect,当您播放视频或YouTube视频时,导航栏将被图像替换。我读了一些帖子,说这导致他们的应用程序被拒绝。

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{
   if([self isMemberOfClass:[UINavigationBar class]])
   {
     UIImage *image = [UIImage imageNamed:@"navBarBackground.png"];
     CGContextClip(ctx);
     CGContextTranslateCTM(ctx, 0, image.size.height);
     CGContextScaleCTM(ctx, 1.0, -1.0);
     CGContextDrawImage(ctx,
     CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 
   }
   else 
   {        
     [super drawLayer:layer inContext:ctx];     
   }
}  
@end

如果这篇文章是准确的,那么这种方法应该没问题:
http://developer.apple.com/iphone/library/qa/ qa2009/qa1637.html

After an annoying night, I found a slight tweak to this, if you use drawLayer. With drawRect, when you play a video, or youtube video, the navbar is replaced with the image. And I read a few posts that this caused their app to be rejected.

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{
   if([self isMemberOfClass:[UINavigationBar class]])
   {
     UIImage *image = [UIImage imageNamed:@"navBarBackground.png"];
     CGContextClip(ctx);
     CGContextTranslateCTM(ctx, 0, image.size.height);
     CGContextScaleCTM(ctx, 1.0, -1.0);
     CGContextDrawImage(ctx,
     CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 
   }
   else 
   {        
     [super drawLayer:layer inContext:ctx];     
   }
}  
@end

If this article is accurate, all should be fine with this approach:
http://developer.apple.com/iphone/library/qa/qa2009/qa1637.html

我也只是我 2024-08-10 10:51:36

简而言之,Apple 不支持修改 UINavigationBar 的结构。他们真的不希望你做你想做的事。这就是导致您所看到的问题的原因。

请提交一个请求此功能的雷达,以便它能够得到足够的关注,以便在某个时候正式添加。

话虽如此,为了解决这个问题,您可以使用 -drawRect: 方法向 UINavigationBar 添加一个类别并在该方法中绘制背景图像。像这样的事情会起作用:

- (void)drawRect:(CGRect)rect
{
  static UIImage *image;
  if (!image) {
    image = [UIImage imageNamed: @"HeaderBackground.png"];
    if (!image) image = [UIImage imageNamed:@"DefaultHeader.png"];
  }
  if (!image) return;
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}

The short answer is that modifying the structure of the UINavigationBar is not supported by Apple. They really do not want you do be doing what you are trying to do. That is what is causing the issue you are seeing.

Please file a radar requesting this feature so that it can get enough attention to be officially added at some point.

Having said that, to solve the issue you can add a category to UINavigationBar with a -drawRect: method and draw the background image in that method. Something like this will work:

- (void)drawRect:(CGRect)rect
{
  static UIImage *image;
  if (!image) {
    image = [UIImage imageNamed: @"HeaderBackground.png"];
    if (!image) image = [UIImage imageNamed:@"DefaultHeader.png"];
  }
  if (!image) return;
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文