2013-11-01 123 views

回答

2

在你的控制器的viewDidLoad方法中添加以下代码..

UIImage *leftbuttonImage = [UIImage imageNamed:@"yourimagename"]; 
     UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [leftbutton setImage:leftbuttonImage forState:UIControlStateNormal]; 
     leftbutton.frame = CGRectMake(0, 0, 35, 35); 
     [leftbutton addTarget:self action:@selector(showLeftMenuPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     UIBarButtonItem *customBarleftItem = [[UIBarButtonItem alloc] initWithCustomView:leftbutton]; 

     self.navigationItem.leftBarButtonItem = customBarleftItem; 
+0

你好,谢谢你的回复。在“[leftbutton addTarget:self action:@selector(showLeftMenuPressed :) forControlEvents:UIControlEventTouchUpInside];”中,xcode表示showLeftMenuPressed未声明。我怎样才能摆脱它? – Zheng

+1

对不起,没有提及..取代showLeftMenuPressed:方法由你想要调用返回按钮单击您的方法。 –

+0

请注意,如果您替换后退按钮,则在iOS 7中返回的滑动手势将停止工作。一些解决方案在这里:http://stackoverflow.com/questions/19054625/changing-back-button-in-ios-7-disables-swipe-to-navigate-back –

2
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"] ; 
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal]; 
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside]; 
backBtn.frame = CGRectMake(0, 0, 54, 30); 
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ; 
self.navigationItem.leftBarButtonItem = backButton; 

,并宣布GoBack的:

- (void)goback 
{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
2
-(void)setUpNAvigationBackBarButton 
{ 

    UIImage* image3 = [UIImage imageNamed:@"icon_back.png"]; 
    CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height); 
    UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg]; 
    [someButton setBackgroundImage:image3 forState:UIControlStateNormal]; 
    [someButton addTarget:self action:@selector(YourMethod which you wanna call on back button 
     forControlEvents:UIControlEventTouchUpInside]; 


    UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton]; 
    [self.navigationItem setLeftBarButtonItem:mailbutton animated:YES]; 
} 

只是调用这个函数在您的viewDidLoad中。 我在ma代码中使用了相同的这个函数。工作很好..你可以使用它。

0

斯威夫特版本: -

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "[email protected]") 
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "[email protected]") 
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil) 

把这个viewDidLoad中()