2012-05-07 10 views
1

您好我檢查了一些帖子,但找不到任何有關我的問題的信息。我已經成功添加了一個按鈕,但有時候會出現後退按鈕。在uinavigationbar iPhone上設置自定義後退按鈕,某些時候返回按鈕的結果在頂部

我的代碼:

self.navigationItem.leftBarButtonItem = nil; 
self.navigationItem.backBarButtonItem = nil; 
[self.navigationItem setHidesBackButton:YES]; 

UIButton *btn = [UIButton backButton]; 
[btn addTarget:self action:@selector(popView) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn]; 
self.navigationItem.leftBarButtonItem = barBtn; 
[barBtn release]; 

結果:

enter image description here

所以也許有人有一些類似的問題,或者知道我做錯了什麼?

在此先感謝!

+1

你在什麼時候調用這段代碼?它應該在'viewWillAppear'上。 – adig

+0

此鏈接適用於我:http://idevrecipes.com/2011/01/12/how-do-iphone-apps-instagramreederdailybooth-implement-custom-navigationbar-with-variable-width-back-buttons/,http: //www.applausible.com/blog/?p=401 –

+0

@adig我在viewdidload中添加了這個,但現在當我移動到viewwillappear它也沒有幫助.. – Lukas

回答

0

最後我找到了那是什麼。導航控制器和顯示視圖控制器存在一些問題,因此修復它們後,此錯誤消失。

1

您需要更改一些代碼...我在這裏放了一些代碼。

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

     [leftButton release]; 

希望,這可能會幫助你..

+0

我有一個類別,即時通訊使用它爲我返回一個按鈕: UIButton * btn = [UIButton backButton]; 一切都是一樣的,謝謝你試圖幫助,但不幸的是,這並沒有解決我的問題。 – Lukas

+0

是的..但它是好的,如果你使用這個定製。你只需要敲響按鈕圖像,這將傳達很多時間 – Nit

+0

我使用了很多這種後扣,所以我做了一個分類,分類節省了我很多時間,當我需要這個按鈕時,我只是寫了UIButton * btn = [UIButton backButton];這就是它。 – Lukas

1

self.navigationItem.hidesBackButton = YES;

應該解決您的問題

+2

但這不是一樣嗎? [self.navigationItem setHidesBackButton:YES]; – Lukas

+0

嗯,它應該實際上是相同的..奇怪,它仍然顯示! – Manuel

1

嗨盧卡斯您可以使用下面的代碼,因爲它工作正常,我....

UINavigationBar的*欄; UIImage * buttonImage = [UIImage imageNamed:@「bak.png」]; UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:buttonImage forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(popView) forControlEvents:UIControlEventTouchUpInside]; 

    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height); 

    customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    self.navigationItem.leftBarButtonItem=customBarItem; 
    [bar addSubview:button]; 

讓我知道如果你需要任何澄清....

相關問題