簡體   English   中英

如何在 iOS 中禁用導航欄按鈕項

[英]how to disable a navigation bar button item in iOS

我創建了一個導航控制器。 在第二個視圖(被推送)中,我有一些網絡服務調用並放置了一個覆蓋視圖和設置

self.view.userInteractionEnabled = NO ;

Web 服務調用完成后,我將恢復到

self.view.userInteractionEnabled = YES ;

當我這樣做時,除導航欄上的按鈕外,所有其他按鈕都被禁用。 如何禁用這兩個導航欄按鈕項? (一個類似於后退按鈕的按鈕,它彈出到第一個視圖控制器和另一個提供有關幫助信息的按鈕)。

我試過使用self.navigationItem.backBarButtonItem.enabled = NO 但我仍然可以點擊按鈕並導航到第一個屏幕。 如何禁用這兩個按鈕?

嘗試這個

受到推崇的

self.navigationItem.leftBarButtonItem.enabled = NO;

self.navigationItem.rightBarButtonItem.enabled = NO;

或簡單地禁用

在邊緣情況下

self.view.window.userInteractionEnabled = NO;

更新:最近蘋果不允許后退按鈕啟用/禁用。 相反,我們可以隱藏它。

self.navigationItem.hidesBackButton = YES;

如果您在Swift上運行,您可以執行以下操作

self.navigationItem.rightBarButtonItem?.enabled = true

此代碼段將禁用該按鈕。

只需禁用您的UINavigationController視圖和導航欄交互:

self.navigationController.navigationBar.userInteractionEnabled = NO;
self.navigationController.view.userInteractionEnabled = NO;

並在需要時啟用它:

self.navigationController.navigationBar.userInteractionEnabled = YES;
self.navigationController.view.userInteractionEnabled = YES;

最新 Swift:要隱藏后退按鈕,您必須使用:

self.navigationItem.setHidesBackButton(true, animated: false)

注意:這可能會觸發導航欄中的錯誤,當轉換到沒有后退按鈕(或在其位置有左按鈕)的視圖時,可能會導致出現一個工件來代替隱藏的后退按鈕。 出現的工件要么是省略號“...”,要么是堆棧上前一個 viewController 的標題。 我相信這個錯誤與蘋果自己的示例代碼項目“CustomizingUINavigationBar”中記錄的錯誤有關,CustomBackButtonViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
}

此代碼應該適用於 Swift 4.2

self.navigationItem.rightBarButtonItem?.isEnabled = false

上面的代碼將禁用按鈕。 要啟用它,將布爾值切換為 true

試試這個代碼:

UIApplication.sharedApplication().beginIgnoringInteractionEvents()

這將阻止用戶與應用程序交互,並在服務調用后再次編寫此代碼:

UIApplication.sharedApplication().endIgnoringInteractionEvents()

當然這會有所幫助。

真正禁用 UIBarButtonItem 的最簡單方法如下:

barButtonVar.isEnabled = false

我通過向我的視圖控制器添加一個屬性來解決這個問題:

@property (nonatomic, strong) IBOutlet UIBarButtonItem * RightButton;

然后我將它連接到故事板上的按鈕。 然后,您可以隨意設置其屬性,例如:

self.RightButton.enabled=true;

為 Swift 3 更新:

如果您想禁用導航欄按鈕項目或您想禁用孔 UINavigationBar 即導航欄上存在的所有項目,請使用以下代碼行;

// if you want disable
self.navigationController?.navigationBar.isUserInteractionEnabled = false

// if you want enable again
self.navigationController?.navigationBar.isUserInteractionEnabled = true

享受...!

對於 iOS 10.3 版本,swift 3:

self.navigationItem.rightBarButtonItem?.isEnabled = false.

一條線解決方案

self.navigationItem.leftBarButtonItem.enabled = NO;

self.navigationItem.rightBarButtonItem.enabled = NO;

斯威夫特 5 和 iOS 13:

要刪除所有左按鈕或僅刪除指定的按鈕,只需從leftBarButtonItems數組中刪除。

        self.navigationItem.leftBarButtonItems = []

導航欄按鈕項目必須通過 navigationItem 屬性引用它們來切換。

例如:

func setupNav() {
    let saveButton = UIBarButtonItem.init(barButtonSystemItem: .save, target: self, action: #selector(onSavePressed))
    navigationItem.rightBarButtonItem = saveButton
    saveButton.isEnabled = false
  }

func validateSave() {
     saveButton.isEnabled = isConditionMet // WON'T work
      navigationItem.rightBarButtonItem.isEnabled = isConditionMet // WORKS!
}
var menuBtn = new UIButton(UIButtonType.Custom);
menuBtn.Frame = new CGRect(x: 0.0, y: 0.0, width: 20, height: 20);
menuBtn.SetImage(new UIImage("filter"), UIControlState.Normal);

menuBtn.Alpha = 0.05f;   //to set the Alpha

menuBtn.Enabled = false;

僅在 Mvvmcross Xamarin.iOS 上測試

Swift 5 它適用於導航控制器

//禁用

self.navigationController?.navigationBar.isUserInteractionEnabled = false

//使能夠

self.navigationController?.navigationBar.isUserInteractionEnabled = true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM