导航栏背景隐藏了右键,无法在下次查看时将其删除

发布于 2024-12-07 13:26:49 字数 1231 浏览 0 评论 0原文

我正在使用下面的类别以编程方式将导航栏背景更改为我的应用程序。它工作正常,但我看不到我在导航栏中添加的右侧按钮。该按钮似乎隐藏,因为点击右侧导航栏仍然会触发连接到导航栏右侧按钮的操作。另外,如何删除使用给定类别方法添加的导航背景?

UINavigationBar+CustomImage.h

#import <Foundation/Foundation.h>

@interface UINavigationBar (CustomImage)
  - (void)setBackgroundImage:(UIImage *)image;
  - (void)clearBackgroundImage;

UINavigationBar+CustomImage.m

#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)
{
  if(image==nil) return;

  UIImageView *image_view = [[UIImageView alloc] initWithImage:image];
  image_view.frame = CGRectMake(0,0,320,44);
  [self addSubview:image_view];
}

- (void)clearBackgroundImage
{
  NSSArray *subviews = [self subviews];
  for(int i=0;i<[subviews count];i++)
  {
     if([[subviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
     {
         [[subviews objectAtIndex:i] removeFromSuperview];
     }
  }

}

在我的视图控制器中它的调用方式如下:

[[self.navigationController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:[UIImage imageNamed:@"header-background.png"]];

感谢您的帮助,

Stephane

I'm using the category below to programmatically change the navigation bar background into my app. It works fine but I cannot see the right button I added in the nav bar. The button seems hidden as tapping on right side nav bar still trigger the action connected to the nav bar right button. Also, how do I remove the navigation background that was added with the given category method ?

UINavigationBar+CustomImage.h

#import <Foundation/Foundation.h>

@interface UINavigationBar (CustomImage)
  - (void)setBackgroundImage:(UIImage *)image;
  - (void)clearBackgroundImage;

UINavigationBar+CustomImage.m

#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)
{
  if(image==nil) return;

  UIImageView *image_view = [[UIImageView alloc] initWithImage:image];
  image_view.frame = CGRectMake(0,0,320,44);
  [self addSubview:image_view];
}

- (void)clearBackgroundImage
{
  NSSArray *subviews = [self subviews];
  for(int i=0;i<[subviews count];i++)
  {
     if([[subviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
     {
         [[subviews objectAtIndex:i] removeFromSuperview];
     }
  }

}

And in my view controller it's called like this:

[[self.navigationController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:[UIImage imageNamed:@"header-background.png"]];

Thanks for helping,

Stephane

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

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

发布评论

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

评论(1

夏花。依旧 2024-12-14 13:26:49

尝试将您的视图发送到后面:

[self sendSubviewToBack:image_view];

将其放置在特定的 z-index:

[self insertSubview:image_view atIndex:0];

或另一个视图下方:

[self insertSubview:image_view belowSubview:anotherSubview];

或另一个视图上方:

[self insertSubview:image_view aboveSubview:anotherSubview];

try sending your view to the back:

[self sendSubviewToBack:image_view];

placing it at a specific z-index:

[self insertSubview:image_view atIndex:0];

or below another view:

[self insertSubview:image_view belowSubview:anotherSubview];

or above another view:

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