2016-02-16 117 views
0

我試圖用一個自定義的返回按鈕UINavigationController但圖像沒有出現。自定義後退按鈕上NavigationController沒有出現

screenshot

下面的代碼:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) 
     self.navigationController?.navigationBar.shadowImage = UIImage() 
     self.navigationController?.navigationBar.translucent = true 
     self.navigationController!.view.backgroundColor = UIColor.clearColor() 

     self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "Back-50") 
     self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Back-50") 
     self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil) 

我不知道發生了什麼,此代碼會錯。

+0

哪個視圖控制器你期待出現的按鈕? –

+0

Root ViewController @ A-Live以外的所有控制器 – user2695433

+0

然後它似乎工作。 –

回答

2

也許,你應該使用UIAppearance。

像這樣

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    [[UINavigationBar appearance] setBackIndicatorImage: [UIImage imageNamed:@"icon-back"]]; 
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage: [UIImage imageNamed:@"icon-back"]]; 

    return YES; 
} 

在迅速

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "") 
    UINavigationBar.appearance().backIndicatorImage = UIImage(named: "") 

    return true 
} 
+0

非常感謝你的工作phewww .. – user2695433

0

您必須實現的目標的ViewController這意味着你將推動控制器代碼的行,因爲你想定製回來,只有當你推一個視圖控制器到導航控制器棧活動項目按鈕。你的代碼適合我。

0

,如果你嘗試在導航堆棧中得到一個視圖控制器你的代碼將工作,這樣

if let currentViewControllerIndex = self.navigationController?.viewControllers.indexOf(self) where currentViewControllerIndex > 0, 
     let previousViewController = self.navigationController?.viewControllers[currentViewControllerIndex - 1] { 

      previousViewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil) 
      self.navigationController?.navigationBar.backIndicatorImage = image 
      self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = image 
    } 
+0

嘗試運氣不好:( – user2695433

+0

也許,你應該使用UIAppearance。見下面的評論。 –

0
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back-50"), style: .Plain, target: self, action: "back") 

func back(){ 
    self.navigationController?.popViewControllerAnimated(true) 
} 
0

試試這個

self.navigationItem.hidesBackButton = YES; 
     UIButton *btnLeft=[UIButton buttonWithType:UIButtonTypeCustom]; 
     btnLeft.frame=CGRectMake(0, 0, 65, 40); 
     [btnLeft addTarget:self action:@selector(onClickBackBarItem:) forControlEvents:UIControlEventTouchUpInside]; 
     [btnLeft setImage:[UIImage imageNamed:@"image_name"] forState:UIControlStateNormal]; 
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnLeft]; 

-(void)onClickBackBarItem:(id)sender 
{ 
    // ** your stuff ** // 
} 

願這幫助同比

+0

我m實際上是新的IOS環境,可能我有swift代碼 – user2695433

+0

將代碼轉換爲swift - https:// objectivec2swift。com /#/ converter –

0

嘗試這是爲我工作。

var BackButton : UIBarButtonItem = UIBarButtonItem(image: 
UIImage(named: "Back-50"), style: .Plain, target: self, action: "back") 

self.navigationItem.leftBarButtonItem = BackButton 

func back() 
{ 
    // your backcontroller code here.... 
} 
1

如果你只想你的圖像顯示爲後退按鈕然後設置的導航欄的tintColor清除顏色,然後根據設定的UIBarButtonItem作爲控制器的navigationItem的backBarButtonItem的圖像。

let backButton = UIBarButtonItem(image: UIImage(named: "back"), style: .Plain, target: self, action: "back") 
self.navigationController?.navigationBar.tintColor = UIColor.clearColor() 
self.navigationItem.backBarButtonItem = backButton 

這將使默認後退按鈕圖像(<)不會出現(INFACT出現但其鮮明的色彩使不可見),並只顯示後方圖像。

+0

這應該添加爲主視圖控制器或遍歷視圖控制器? – user2695433

+0

謝謝pradeep。我試過了你的代碼,現在顯示返回按鈕圖像。但另一個Button圖像(如圖中所示)也同時出現。 – user2695433

+0

在您附加的圖片中,您不清楚最新的後退按鈕圖像。你能指定它的實際出現方式以及你希望它如何實際出現嗎? –

相關問題