Iphone UINavigationBar子视图隐藏了右UIBarButtonim

Iphone UINavigationBar子视图隐藏了右UIBarButtonim,iphone,uinavigationcontroller,uinavigationbar,uinavigationitem,Iphone,Uinavigationcontroller,Uinavigationbar,Uinavigationitem,在根表视图控制器中,我添加了一个子视图,用于保存图像: [self.navigationController.navigationBar addSubview:imageView]; 然后,在我推到堆栈上的子表视图控制器中,我设置了一个右UIBarButtonim: UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain tar

在根表视图控制器中,我添加了一个子视图,用于保存图像:

[self.navigationController.navigationBar addSubview:imageView];
然后,在我推到堆栈上的子表视图控制器中,我设置了一个右UIBarButtonim:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Right"  style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
我无法理解为什么导航栏中没有显示该按钮。如果我点击(空白处)rightAction:方法被调用,因此按钮“在”那里,预期它不会显示

我已尝试在导航栏中将imageView子视图发送回,但无效。这有点道理,因为按钮实际上属于UINavigationItem

有人知道我怎样才能正确显示那个按钮吗


更新:后退按钮确实显示正确,但它是由系统添加的…

将图像添加到
UINavigationBar
的更好方法是将其子类化或创建一个类别,并覆盖
drawRect
方法:

//                                  #Lighter r,g,b,a            #Darker r,g,b,a
#define MAIN_COLOR_COMPONENTS       { 0.153, 0.306, 0.553, 1.0, 0.122, 0.247, 0.482, 1.0 }
#define LIGHT_COLOR_COMPONENTS      { 0.478, 0.573, 0.725, 1.0, 0.216, 0.357, 0.584, 1.0 }

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
    if (imageReady) {
        UIImage *img = [UIImage imageNamed: @"navigation_background.png"];
        [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    } else {
        // Render yourself instead.
        // You will need to adjust the MAIN_COLOR_COMPONENTS and LIGHT_COLOR_COMPONENTS to match your app

       // emulate the tint colored bar
       CGContextRef context = UIGraphicsGetCurrentContext();
       CGFloat locations[2] = { 0.0, 1.0 };
       CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();

       CGFloat topComponents[8] = LIGHT_COLOR_COMPONENTS;
       CGGradientRef topGradient = CGGradientCreateWithColorComponents(myColorspace, topComponents, locations, 2);
       CGContextDrawLinearGradient(context, topGradient, CGPointMake(0, 0), CGPointMake(0,self.frame.size.height/2), 0);
       CGGradientRelease(topGradient);

       CGFloat botComponents[8] = MAIN_COLOR_COMPONENTS;
       CGGradientRef botGradient = CGGradientCreateWithColorComponents(myColorspace, botComponents, locations, 2);
       CGContextDrawLinearGradient(context, botGradient,
       CGPointMake(0,self.frame.size.height/2), CGPointMake(0, self.frame.size.height), 0);
       CGGradientRelease(botGradient);

       CGColorSpaceRelease(myColorspace);


       // top Line
       CGContextSetRGBStrokeColor(context, 1, 1, 1, 1.0);
       CGContextMoveToPoint(context, 0, 0);
       CGContextAddLineToPoint(context, self.frame.size.width, 0);
       CGContextStrokePath(context);

       // bottom line
       CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
       CGContextMoveToPoint(context, 0, self.frame.size.height);
       CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
       CGContextStrokePath(context);
    }
}

@end

这样做会使您的按钮显示正确且不太“粗糙”。

将图像添加到
UINavigationBar
的更好方法是对其进行子类化或创建类别,并覆盖
drawRect
方法:

//                                  #Lighter r,g,b,a            #Darker r,g,b,a
#define MAIN_COLOR_COMPONENTS       { 0.153, 0.306, 0.553, 1.0, 0.122, 0.247, 0.482, 1.0 }
#define LIGHT_COLOR_COMPONENTS      { 0.478, 0.573, 0.725, 1.0, 0.216, 0.357, 0.584, 1.0 }

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
    if (imageReady) {
        UIImage *img = [UIImage imageNamed: @"navigation_background.png"];
        [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    } else {
        // Render yourself instead.
        // You will need to adjust the MAIN_COLOR_COMPONENTS and LIGHT_COLOR_COMPONENTS to match your app

       // emulate the tint colored bar
       CGContextRef context = UIGraphicsGetCurrentContext();
       CGFloat locations[2] = { 0.0, 1.0 };
       CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();

       CGFloat topComponents[8] = LIGHT_COLOR_COMPONENTS;
       CGGradientRef topGradient = CGGradientCreateWithColorComponents(myColorspace, topComponents, locations, 2);
       CGContextDrawLinearGradient(context, topGradient, CGPointMake(0, 0), CGPointMake(0,self.frame.size.height/2), 0);
       CGGradientRelease(topGradient);

       CGFloat botComponents[8] = MAIN_COLOR_COMPONENTS;
       CGGradientRef botGradient = CGGradientCreateWithColorComponents(myColorspace, botComponents, locations, 2);
       CGContextDrawLinearGradient(context, botGradient,
       CGPointMake(0,self.frame.size.height/2), CGPointMake(0, self.frame.size.height), 0);
       CGGradientRelease(botGradient);

       CGColorSpaceRelease(myColorspace);


       // top Line
       CGContextSetRGBStrokeColor(context, 1, 1, 1, 1.0);
       CGContextMoveToPoint(context, 0, 0);
       CGContextAddLineToPoint(context, self.frame.size.width, 0);
       CGContextStrokePath(context);

       // bottom line
       CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
       CGContextMoveToPoint(context, 0, self.frame.size.height);
       CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
       CGContextStrokePath(context);
    }
}

@end

这样做会使您的按钮显示正确,而且不太“粗糙”。

我认为您应该将导航项目的标题视图设置为imageView

self.navigationItem.titleView = imageView;
我猜
UINavigationBar
在子视图中显示
UINavigationItem
s,并且您将
UIImageView
添加到该子视图的顶部


在我的一个视图中,我在旁边显示了一个标签和一个图像。我想我刚刚将标签和图像视图添加到了一个视图中,然后用于标题视图。

我想您应该将导航项的标题视图设置为imageView

self.navigationItem.titleView = imageView;
我猜
UINavigationBar
在子视图中显示
UINavigationItem
s,并且您将
UIImageView
添加到该子视图的顶部


在我的一个视图中,我在旁边显示了一个标签和一个图像。我想我刚刚将标签和图像视图添加到一个视图中,然后用于标题视图。

我可以将UINavigationBar子类化,但该属性在UINavigationController中是只读的。。。另一件事是“图像”实际上是从服务器检索的,因此必须动态设置。我接受了你的回答,但我非常感谢你的以下输入:如果我的图像在显示导航栏时没有准备好,我会得到一个黑色背景。任何更改我都可以还原为默认的蓝色背景/样式,而不使用图像?@coneybeare hai我遇到了相同的问题我使用了您的代码片段仍然存在相同的问题,请您提供帮助me@balu,除非您使用子类,否则这在iOS 5中不再有效。我可以将UINavigationBar子类化,但该属性在UINavigationController中是只读的。。。另一件事是“图像”实际上是从服务器检索的,因此必须动态设置。我接受了你的回答,但我非常感谢你的以下输入:如果我的图像在显示导航栏时没有准备好,我会得到一个黑色背景。任何更改我都可以还原为默认的蓝色背景/样式,而不使用图像?@coneybeare hai我遇到了相同的问题我使用了您的代码片段仍然存在相同的问题,请您提供帮助me@balu,这在iOS 5中不再有效,除非您使用子类。这通常可以工作,但标题视图不会覆盖整个屏幕。我猜它只有300像素而不是320像素。我猜你想把它作为背景图像吗?好的,那么可以尝试在索引0(-insertSubview:atIndex:)处添加imageView,这通常可以正常工作,但标题视图不会跨越整个屏幕。我猜它只有300像素而不是320像素。我猜你想把它作为背景图像吗?好的,那么可以尝试将imageView添加到索引0(-insertSubview:atIndex:)