UINavigationBar子视图隐藏了正确的UIBarButtonItem

时间:2009-10-28 02:22:08

标签: iphone uinavigationcontroller uinavigationbar uinavigationitem

在根表视图控制器中,我添加了一个包含图像的子视图:

[self.navigationController.navigationBar addSubview:imageView];

然后在我推入堆栈的子表视图控制器中,我设置了一个正确的UIBarButtonItem:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Right"  style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];

我无法理解导航栏中没有显示按钮的原因。如果我点击(空格),则会调用rightAction:方法,因此按钮“在”那里,期望它不会显示。

我试图将imageView子视图发送回导航栏中,但无济于事。这有点意义,因为按钮实际上属于UINavigationItem ......

有人知道如何正确显示该按钮吗?

更新:后退按钮确实正确显示,但系统会添加它......

2 个答案:

答案 0 :(得分:0)

将图像添加到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

这样做会使你的按钮正确显示而且不那么“hacky”。​​

答案 1 :(得分:0)

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

self.navigationItem.titleView = imageView;

我想UINavigationBar会在子视图中显示UINavigationItem,并且您要在该子视图之上添加UIImageView

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