UINavigationController 的 rightBarButtonItem 中的自定义 UIToolbar 在弹出回视图时消失

发布于 2024-10-03 07:52:24 字数 2954 浏览 0 评论 0原文

编辑2: 我现在已经解决了这个问题。我为 UINavigationBar 设置了一个自定义背景图像,如下所示:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NavigationBarBackground" ofType:@"png"]]];
[navBar addSubview:imgView];
[navBar sendSubviewToBack:imgView];
[imgView release];

这对于 UINavigationController 的视图控制器堆栈上的第一个视图控制器来说很好,但是任何子视图都会使此代码重新运行并将自定义背景放置在前面。

修复: 只需删除导航控制器上自定义背景的所有代码,然后按以下方式扩展 UINavigationBar 类:

创建一个名为 UINavigationBar+CustomImage 的新 .h / .m 文件。将 .h 代码设置为:

@interface UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect;
@end

将 .m 代码设置为:

#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"NavigationBarBackground.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

瞧!现在一切正常。

--- END EDIT 2 ---

我有一个 UINavigationController,它像这样向下钻取:

HOMEPAGE > DETAIL PAGE > MAP VIEW

详细信息页面将一个自定义 UIToolbar(带有三个按钮)添加到 viewDidLoad 中的 rightBarButtonItem 中,如下所示:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 1.0, 157.0, 44.1)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NavigationBarBackground" ofType:@"png"]]];
[tools addSubview:imgView];
[tools sendSubviewToBack:imgView];
[tools setTintColor:[UIColor colorWithRed:127/255.0 green:184/255.0 blue:72/255.0 alpha:1.0]];
[imgView release];

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithTitle:@"Filter" style:UIBarButtonItemStyleBordered target:self action:NULL];

[buttons addObject:bi];
[bi release];

bi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Map.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showMap:)];

[buttons addObject:bi];
[bi release];

bi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Favourite.png"] style:UIBarButtonItemStyleBordered target:self action:NULL];
[buttons addObject:bi];
[bi release];

[tools setItems:buttons animated:NO];

[buttons release];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

这显示完美当查看详细视图页面时,当我转到地图视图时,自定义 UIToolbar 消失(如预期),但是,当返回详细视图时,自定义 UIToolbar 不再可见。不过,它就在那里,因为我仍然可以点击“地图”按钮应该所在的区域,它将我推回到地图视图。

您知道如何让这个 UIToolbar 在返回 DETAIL 视图时再次显示吗?

我尝试过在viewWillAppear中重建自定义UIToolbar,其效果与上面完全相同。

编辑 - 如果我将构建自定义 UIToolbar 的代码放在 viewDidAppear 方法中,则此方法有效。但我不喜欢这个解决方案,当视图向后滑动时,然后会出现自定义的 rightBarButtonItem。这是否与显示页面之前未初始化导航控制器有关?

EDIT 2:
I have now fixed this issue. I was setting a custom background image for my UINavigationBar like so:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NavigationBarBackground" ofType:@"png"]]];
[navBar addSubview:imgView];
[navBar sendSubviewToBack:imgView];
[imgView release];

Which was fine for the first view controller on the UINavigationController's view controller stack, but any sub views were making this code re-run and placing the custom background to the front.

The Fix:
Simply remove all your code for the custom background on the navigation controller, then EXTEND the UINavigationBar class in the following way:

Create a new .h / .m file called UINavigationBar+CustomImage. Set the .h code to:

@interface UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect;
@end

And the .m to:

#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"NavigationBarBackground.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

And voila! Everything works fine now.

--- END EDIT 2 ---

I have a UINavigationController, which drills down like so:

HOMEPAGE > DETAIL PAGE > MAP VIEW

The DETAIL PAGE adds a custom UIToolbar (with three buttons) to the rightBarButtonItem within viewDidLoad like so:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 1.0, 157.0, 44.1)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NavigationBarBackground" ofType:@"png"]]];
[tools addSubview:imgView];
[tools sendSubviewToBack:imgView];
[tools setTintColor:[UIColor colorWithRed:127/255.0 green:184/255.0 blue:72/255.0 alpha:1.0]];
[imgView release];

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithTitle:@"Filter" style:UIBarButtonItemStyleBordered target:self action:NULL];

[buttons addObject:bi];
[bi release];

bi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Map.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showMap:)];

[buttons addObject:bi];
[bi release];

bi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Favourite.png"] style:UIBarButtonItemStyleBordered target:self action:NULL];
[buttons addObject:bi];
[bi release];

[tools setItems:buttons animated:NO];

[buttons release];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

This displays perfectly when viewing the DETAIL VIEW page, and when I go to the MAP VIEW, the custom UIToolbar disappears (as expected), however, when going back to the DETAIL VIEW, the custom UIToolbar is no longer visible. It is there, though, as I can still tap the area where the 'Map' button should be, and it pushes me back to the MAP VIEW.

Would you know how I can get this UIToolbar to display again when going back to the DETAIL view?

I have tried rebuilding the custom UIToolbar in viewWillAppear, which has the exact same effects as above.

EDIT - This works if I put the code that builds the custom UIToolbar within the viewDidAppear method. I don't like this solution though, as the view slides back, then the custom rightBarButtonItem appears. Is this something to do with the navigation controller not being initalised before the page is displayed?

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

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

发布评论

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

评论(2

傲世九天 2024-10-10 07:52:24

如果它在 viewDidAppear 中有效,你可以尝试 viewWillAppear。一般来说,我从未见过将 UIToolbar 设置为 UIBarButtonItem,因此我不一定期望标准行为。让我知道进展如何。

If it works in viewDidAppear you could try viewWillAppear. In general, I've never seen a UIToolbar set as a UIBarButtonItem, so I wouldn't necessarily expect standard behavior. Let me know how it goes.

岁吢 2024-10-10 07:52:24

当您尝试做任何稍微不寻常的事情(即使是应该工作的事情)时,Apple UI API 经常以不寻常的方式失败,并且尝试将 UIToolbar 插入 rightBarButtonItem 绝对属于我对“不寻常”的定义。

您真正应该做的是隐藏导航栏并将 UIToolbar 添加为视图控制器主视图的子视图。或者,您可以完全避免 UIToolbar 并在自定义视图中自己实现按钮。

Apple UI APIs often fail in unusual ways when you try to do anything even slightly unusual (even things that should work), and trying to insert a UIToolbar into a rightBarButtonItem definitely falls into my definition of "unusual".

What you really should be doing is hiding the navigation bar and adding the UIToolbar as a subview of your view controller's main view. Alternatively you could avoid UIToolbar completely and implement the buttons yourself in a custom view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文