2013-11-01 152 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中()