2015-12-15 79 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