为 UINavigationBar 或标题设置自定义图像:导航期间混乱
我找到了足够多的好例子,说明如何使用自定义图像作为 UINavigationBar 的背景,而无需在不同视图之间导航之后或期间隐藏任何按钮。但是,我有以下问题,我似乎没有答案。
我有一个 UITabBarViewController 对象作为根控制器,向其中添加了许多不同的 UINavigationControllers 对象作为子视图(每个选项卡一个),每个子视图都使用 UITableViewController 对象作为根控制器进行初始化。从每个表视图中,可以导航到子控制器,该子控制器显示有关所选单元格的更多详细信息。现在我遇到的问题如下:
在每个表视图的导航控制器中,我设置了自定义背景图像。这是通过覆盖 UINavigationBar 的 drawRect 方法来完成的。当用户向下导航到每个表视图的详细信息视图时,图像应替换为详细信息视图的标题。我通过使用全局变量来实现此目的,并在每个视图的每个 viewWillAppear 方法中更新它,并在覆盖的 drawRect 方法中使用 if 条件。因此,用一些代码来说明这一点:
在应用程序委托 .h.file 中,我将外部变量声明为:
extern BOOL displayImage;
在应用程序委托 .m 文件中,我将外部变量初始化为:
BOOL displayImage = TRUE;
对于所有视图控制器,viewWillAppear 方法的实现如下
// View where I want to show the image in the navigation bar
- (void)viewWillAppear:(BOOL)animated {
...
self.navigationItem.title = nil; // No title
displayImage = TRUE;
[self.navigationController.navigationBar setNeedsDisplay];
...
}
// View where I do NOT want to show the image in the navigation bar
- (void)viewWillAppear:(BOOL)animated {
...
self.title = @"Info";
displayImage = FALSE;
[self.navigationController.navigationBar setNeedsDisplay];
...
}
// Overwritten drawRect method of UINavigationBar
- (void)drawRect:(CGRect)rect {
if (displayImage) {
// Code that draws the image by calling drawInRect
} else {
// Code that just sets a black background
}
}
:当我从表视图导航到详细视图(表视图的子控制器)并返回父视图时,效果非常好。但是,当我从详细信息视图导航到选项卡栏的另一个选项卡时,详细信息视图的标题消失并且图像重新出现(到目前为止一切顺利),但是当我然后导航回包含详细信息视图的选项卡时,详细视图的标题出现,但图像不会消失(即标题和图像都显示在导航栏中)。在这种情况下似乎没有调用 setNeedsDisplay 方法。在此期间,外部变量得到正确更新。
有人可以告诉我我在这里可能做错了什么吗?
谢谢
I found enough good examples about how to use a custom image as a background for a UINavigationBar without hiding any buttons after or during navigating between different views. However, I have the following problem I don't seem to have an answer for.
I have a UITabBarViewController object as a root controller to which I have added a number of different UINavigationControllers objects as subviews (one per tab) each initialized with a UITableViewController object as their root controller. From each table view it is possible to navigate to a child controller which displays more detailed information about the selected cell. Now the problem i have is the following:
In the navigation controller of each table view I set a custom background image. This is done by overwriting the drawRect method of UINavigationBar. When a user navigates down to the detail view of each table view, the image should be replaced with the title of the detail view. I accomplish this by using a global variable and I update this in each viewWillAppear method of every view and using an if condition inside the overwritten drawRect method. So to illustrate this with some code:
In the app delegate .h.file I declare the external variable as:
extern BOOL displayImage;
In the app delegate .m file I initialize the external variable as:
BOOL displayImage = TRUE;
For all view controllers the viewWillAppear method is implemented like:
// View where I want to show the image in the navigation bar
- (void)viewWillAppear:(BOOL)animated {
...
self.navigationItem.title = nil; // No title
displayImage = TRUE;
[self.navigationController.navigationBar setNeedsDisplay];
...
}
// View where I do NOT want to show the image in the navigation bar
- (void)viewWillAppear:(BOOL)animated {
...
self.title = @"Info";
displayImage = FALSE;
[self.navigationController.navigationBar setNeedsDisplay];
...
}
// Overwritten drawRect method of UINavigationBar
- (void)drawRect:(CGRect)rect {
if (displayImage) {
// Code that draws the image by calling drawInRect
} else {
// Code that just sets a black background
}
}
This works great when I navigate from a table view to the detail view (child controller of the table view) and back to the parent view. However, when I navigate from a detail view to another tab of the tab bar, the title of the detail view dissapears and the image re-appears (so far so good), but when I then navigate back to the tab holding the detail view, the title of the detail view appears, but the image does not dissapear (i.e. BOTH the title and image are shown in the navigation bar). It seems like the setNeedsDisplay method is not called in this situation. During all this the external variable gets updated correctly.
Is there anybody who can tell me what I might be doing wrong here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到同样的情况。如果我遇到您的问题,则在不同选项卡之间切换时,导航栏中的图像不会消失。因此,您需要创建两张图像,一张是普通图像,另一张是黑色图像。
使用黑色图像而不是在类别中的drawrect方法的else部分中设置黑色。并且在从页面切换时也会重置 viewWillDisappear 中的变量,而不是在下一页上设置变量。也许你的问题得到解决。
I was also in the same situation. If I m getting your problem your image from navigation bar is not getting disappear when switching between the different tabs. So you need to create two images, one normal images another one is black image.
Use black image instead of setting black color in the else part of the drawrect method in the category. And also reset the variable in the viewWillDisappear when switching from the page instead of setting the variable on the next page. May be your problem get solved.