2015-12-15 142 views
0

作为我的头衔,我浏览了几个网站,但未能取得成效。不过,我成功地在导航栏上添加了一个图像。这是我目前面临的两个问题,都在UINavigationBar部分。 1.无法显示rightbarbutton 2.无法隐藏后退箭头 “<”UINavigationItem rightbarbuttonitem not displayed

MasterViewController.m

//Back Button 
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
UIImage *backBtnImage = [UIImage imageNamed:@"sidemenu.png"]; 
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal]; 
backBtn.frame = CGRectMake(0, 0, 54, 30); 

//Rightbarbutton 
UIButton *infoButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_info.png"]]; 
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton]; 
self.navigationItem.rightBarButtonItem = infoButtonItem; 

//Image (Working and displaying) 
UIImage *image = [UIImage imageNamed: @"icon_image.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(245, 0, 30, 42)]; 
[imageView setImage:image]; 
[self.navigationController.navigationBar addSubview:imageView]; 

回答

0

这是您的UINavigationItem + AtAdditions.h文件

#import <UIKit/UIKit.h> 

typedef NS_ENUM(NSInteger, ATNavigationItemButtonSide) { 
    ATNavigationItemButtonSideRight, 
    ATNavigationItemButtonSideLeft 
}; 

@interface UINavigationItem(ATAdditions) 

// This method adds an image-only button to the requested size (right or left) of the current navigation item, the image can't be tapped 
- (void)addColorfulImage:(NSString*)imageName toSide:(ATNavigationItemButtonSide)side; 

@end 

这是你的UINavigationItem + AtAdditions.m文件

#import "UINavigationItem+ATAdditions.h" 

@implementation UINavigationItem(ATAdditions) 

- (void)addColorfulImage:(NSString*)imageName toSide:(ATNavigationItemButtonSide)side { 
    // Create a button with the required image on it 
    UIImage *image = [UIImage imageNamed:imageName]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.bounds = CGRectMake(0, 0, image.size.width, image.size.height); 
    [button setImage:image forState:UIControlStateNormal]; 

    // Stop the events (it's an image only) 
    button.userInteractionEnabled = NO; 

    // Add the button to the navigation item 
    if (side == ATNavigationItemButtonSideLeft) { 
     self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    } 
    else if (side == ATNavigationItemButtonSideRight) { 
     self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    } 
} 

@end 

稍后,您可以导入.h文件并将其用于视图控制器中的导航项目。

+0

相同的程序是bringSubviewToFront?正如我刚刚“没有可见@interface for'UINavigationBar'声明选择器'bringSubviewToBack:”。 – aka

+0

对不起,我编辑了答案,这是sendSubviewToBack –

+0

它不工作... – aka

0

我想你应该初始化您rightItem的内容作为一个的UIButton,而不是从ImageView的到的UIButton铸造...所以你这样做是为了返回项目..

+0

我试过了,按钮仍然没有显示。 – aka