UINavigationBar自定义背景问题

发布于 2024-08-04 20:15:19 字数 407 浏览 7 评论 0原文

我已经成功地通过使用以下方法向导航栏添加了自定义背景:

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBar.png"]];
[myViewController.navigationBar insertSubview:iv atIndex:0];
[iv release];

这工作正常,我可以正常看到标题和按钮。但是,当我向下钻取一层然后返回到根时,按钮被隐藏,但标题仍然可见。导航栏图像似乎覆盖了按钮项目。

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

有什么想法吗?

谢谢,

迈克

I've managed to add a custom background to my navigation bar by using:

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBar.png"]];
[myViewController.navigationBar insertSubview:iv atIndex:0];
[iv release];

This works fine and I can see the title and buttons okay. However, when I drill down one level and then go back to the root, the buttons are hidden, but the title is still visible. It appears the navigation bar image is covering the button items.

I'm confused as I'm inserting it at the bottom so I would assume when the navigation items are pushed and popped that they are displayed above other views in the navigation bar.

Any ideas?

Thanks,

Mike

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

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

发布评论

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

评论(5

七月上 2024-08-11 20:15:19

按照此处的描述,我建议在导航栏中添加一个类别。

@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

Following the description from here, I suggest adding a category to the nav bar.

@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
风吹雪碎 2024-08-11 20:15:19

在 iOS 5 上这不起作用,但好消息是有一个简单的方法可以做到这

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault];

一点注意:这仅适用于 iOS 5 及更高版本,因此如果您想向后兼容,请确保检查 iOS 版本。

On iOS 5 this will not work but the good news is that there is a simple way of doing this

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault];

NOTE: This will only work on iOS 5 and above so make sure you check iOS version if you want to be backwards compatible.

余罪 2024-08-11 20:15:19

NavigationBar 的子视图不断变化,我的意思是每个视图都会出现/消失,因此您不应期望在子视图数组的索引 0 处添加添加的 imageview。

您可以尝试在 viewDidAppear 处添加此内容(在管理导航项的每个视图控制器中/:):

NSLog(@"navbar's subviews before \n %@",[[[self navigationController] navigationBar] subviews]);

for (UIImageView *imgv in [[[self navigationController] navigationBar] subviews]) {
    [[[self navigationController] navigationBar] sendSubviewToBack:imgv];
}

NSLog(@"navbar's subviews after \n %@",[[[self navigationController] navigationBar] subviews]);

我已经使用了 Hauek 的 解决方案,但是当该栏在其他控制器中具有不同的风味时,它可能会出现一些小问题,无论如何,这都不是最好的选择。

希望它有帮助,至少了解为什么你所做的事情不起作用,

NavigationBar's subviews are constantly changing, i mean every view appear/disappear, so you shouldn't expect to have the added imageview at the index 0 of the subviews array.

You could try to add this at viewDidAppear (in every viewcontroller that manage navigation items /:):

NSLog(@"navbar's subviews before \n %@",[[[self navigationController] navigationBar] subviews]);

for (UIImageView *imgv in [[[self navigationController] navigationBar] subviews]) {
    [[[self navigationController] navigationBar] sendSubviewToBack:imgv];
}

NSLog(@"navbar's subviews after \n %@",[[[self navigationController] navigationBar] subviews]);

I have used Hauek's solution but when the bar has diferent flavor in other controllers, it could give some minor problems, anyway, this is neither the best option.

Hope it help, at least to understand why what you were doing wasn't working,

妖妓 2024-08-11 20:15:19

您可以重写 UINavigationBar 类别类中的 drawLayer:inContext: 方法,而不是在 drawRect: 中执行此操作。在drawLayer:inContext:方法中,您可以绘制您想要使用的背景图像。如果您的应用程序支持多种界面方向,您还可以为纵向和横向界面方向选择不同的图像。

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    if ([self isMemberOfClass:[UINavigationBar class]] == NO) {
        return;
    }

    UIImage *image = (self.frame.size.width > 320) ?
                        [UINavigationBar bgImageLandscape] : [UINavigationBar bgImagePortrait];
    CGContextClip(context);
    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}

这个完整的演示 Xcode 项目 自定义 UINavigationBar 的外观也可能会有所帮助。

Instead of doing it in drawRect: you can override the drawLayer:inContext: method in a UINavigationBar category class. Inside the drawLayer:inContext: method, you can draw the background image you want to use. Also you can choose different images for portrait and landscape interface orientations if your app supports multiple interface orientations.

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    if ([self isMemberOfClass:[UINavigationBar class]] == NO) {
        return;
    }

    UIImage *image = (self.frame.size.width > 320) ?
                        [UINavigationBar bgImageLandscape] : [UINavigationBar bgImagePortrait];
    CGContextClip(context);
    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}

This complete demo Xcode project on customizing the appearance of UINavigationBar might also be helpful.

不羁少年 2024-08-11 20:15:19

接下来,您可以简单地向 UINavigationBar 添加新背景。

/////////
 UINavigationController *navController = [[UINavigationController alloc] init];
 UINavigationBar*bar=[navController navigationBar]; // get navigation bar 
  NSArray*barSubview=[bar subviews]; // get all subviews of bar
 UIView*backView=[arr objectAtIndex:0]; // at index 0 lying background view of our bar
 backView.alpha=0.0f;// set alpha to 0 and it will become invisible

    UIImageView*imgView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-bar.png"]];// create the image view that we will insers as subbview in our bar
    imgView.frame=CGRectMake(0, 0, 320, 44);
    [bar insertSubview:imgView atIndex:0];// and it will be out new background
    [imgView release];

The way you can simply add a new background to your UINavigationBar is next.

/////////
 UINavigationController *navController = [[UINavigationController alloc] init];
 UINavigationBar*bar=[navController navigationBar]; // get navigation bar 
  NSArray*barSubview=[bar subviews]; // get all subviews of bar
 UIView*backView=[arr objectAtIndex:0]; // at index 0 lying background view of our bar
 backView.alpha=0.0f;// set alpha to 0 and it will become invisible

    UIImageView*imgView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-bar.png"]];// create the image view that we will insers as subbview in our bar
    imgView.frame=CGRectMake(0, 0, 320, 44);
    [bar insertSubview:imgView atIndex:0];// and it will be out new background
    [imgView release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文