2012-04-23 80 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]; 

背景图片必须是可调整大小的图片才能获得良好效果。