UINavigationBar问题的自定义背景

Mic*_*all 2 iphone uinavigationbar uibarbuttonitem uinavigationitem

我已设法通过以下方式将自定义背景添加到导航栏:

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBar.png"]];
[myViewController.navigationBar insertSubview:iv atIndex:0];
[iv release];
Run Code Online (Sandbox Code Playgroud)

这工作正常,我可以看到标题和按钮没关系.但是,当我向下钻取一个级别然后返回到根时,按钮将被隐藏,但标题仍然可见.导航栏图像显示为覆盖按钮项目.

我很困惑,因为我将它插入底部,所以我会假设当导航项被推动并弹出时它们显示在导航栏中的其他视图上方.

有任何想法吗?

谢谢,

麦克风

Hau*_*uke 12

按照此处的说明,我建议在导航栏中添加一个类别.

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
    // Drawing code 
    UIImage *img = [UIImage imageNamed: @"navbar_background.png"];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, CGRectMake(0, 0, 320, self.frame.size.height), img.CGImage);

}
@end
Run Code Online (Sandbox Code Playgroud)

  • 啊得到了...你需要使用[img drawInRect:CGRectMake(0,0,320,self.frame.size.height)]; 而不是CGContextDrawImage()! (5认同)