2012-04-23 59 views
2

我想改變我的UINavigationBar的背面按鈕使用[UINavigationBar的外觀]改變後退按鈕圖像

我可以使用此代碼做到這一點:

// Set the custom back button 
    UIImage *buttonImage = [UIImage imageNamed:@"backag.png"]; 

    //create the button and assign the image 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setImage:buttonImage forState:UIControlStateNormal]; 
    [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
    button.adjustsImageWhenDisabled = NO; 


    //set the frame of the button to the size of the image (see note below) 
    button.frame = CGRectMake(0, 0, 30, 30); 

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; 

    //create a UIBarButtonItem with the button as a custom view 
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    self.navigationItem.leftBarButtonItem = customBarItem; 
    self.navigationItem.hidesBackButton = YES; 

    // Cleanup 
    [customBarItem release]; 

,但我已經把那每個viewDidLoad方法中的代碼。我想爲整個項目做一次。

我的嘗試是這樣的:

UIImage *buttonBack30 = [[UIImage imageNamed:@"backag"] //16 5 
          resizableImageWithCapInsets:UIEdgeInsetsMake(1000, 1000, 1000, 1000)]; 
    UIImage *buttonBack31 = [[UIImage imageNamed:@"selback"] 
          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; 
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 
                 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
    //[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31 
    //    forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; 
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31 
                 forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; 

    NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; 
    [attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor]; 
    [attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor]; 
    // [attributes setValue:[UIFont fontWithName:@"Helvetica" size:15] forKey:UITextAttributeFont]; 
    [attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset]; 
    [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateHighlighted]; 

但它延伸出來的圖像和文字吸引了它,這是我不想要的。我只是想在每個視圖中顯示相同大小的圖像。

謝謝!

回答

4

這可能工作了。在頂部添加下面的你的appdelegate。

@implementation UIViewController (CustomFeatures) 
-(void)setNavigationBar{ 
    // Set the custom back button 
    UIImage *buttonImage = [UIImage imageNamed:@"backag.png"]; 

    //create the button and assign the image 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setImage:buttonImage forState:UIControlStateNormal]; 
    [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
    button.adjustsImageWhenDisabled = NO; 


    //set the frame of the button to the size of the image (see note below) 
    button.frame = CGRectMake(0, 0, 30, 30); 

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; 

    //create a UIBarButtonItem with the button as a custom view 
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    self.navigationItem.leftBarButtonItem = customBarItem; 
    self.navigationItem.hidesBackButton = YES; 

    // Cleanup 
    [customBarItem release]; 
} 
@end 

,並呼籲[self setNavigationBar];viewDidLoad

+0

工作,但當我在視圖之間來回移動時按鈕閃爍。這並不是什麼大不了的事情,但如果它可以轉換更清潔的 – itgiawa 2012-04-25 00:44:02

+0

以及後面的操作方法在哪裏,那將會很不錯。 – 2015-09-11 09:32:32

+0

不錯的hack!,後退方法swift'self.navigationController?.popViewController(animated:true)' – 2017-03-17 16:00:32

1

使用此代碼,並享受...

UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[leftButton setUserInteractionEnabled:NO]; 
[leftButton setImage:[UIImage imageNamed:@"backag.png"] forState:UIControlStateNormal];  
leftButton.frame = CGRectMake(0, 0, 30, 30); 
[leftButton addTarget:self action:@selector(YourclickeventClick:) forControlEvents:UIControlEventTouchUpInside];   
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton]; 
[leftbutton release]; 

希望,這將幫助你..

23

與iOS 5.0開始,您可以使用全局外觀修飾到整個應用程序更改所有後退按鈕:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"] 
             forState:UIControlStateNormal 
             barMetrics:UIBarMetricsDefault]; 

背景圖片必須是可調整大小的圖片才能獲得良好效果。