Search code examples
iosobjective-cuinavigationcontrolleruinavigationbar

Where to Set NavigationController.NavigationBar.isUserInteractionEnabled


I have a problem about setting to navigationBar.isUserInteractionEnabled to false. I set it in viewDidLoad and viewDidLayoutSubviews. But this setting is not working because The value is true even I set to false. I need to set to false only when I back to previous View (when backButton is clicked and then navigationBar.isUserInteraction should be set to false). How can I do it? there is any other delegate? or I need to set timer after viewDidLoad is called. Here is an image to make it clear what I mean


Solution

  • In case you are specific about disabling userInteraction of navigation bar for some specific reason you can do it by calling

    Swift :

    self.navigationController?.navigationBar.isUserInteractionEnabled = false
    

    Objective-C:

    [self.navigationController.navigationBar setUserInteractionEnabled:false];
    

    Edit 2:

    I need to set to false only when I back to previous View (when backButton is clicked and then navigationBar.isUserInteraction should be set to false)

    You cant disable the navigation bar user interaction in viewWillDisappear of a viewController because if the backButton is tapped by the time viewWillDisappear gets called ViewController is removed from the Navigation stack.

    So when you call self.navigationController your navigation controller is nil.

    Simplest solution, add your own back button and disable the user interaction of navigation bar before actually popping the VC out.

    In your secondVC

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(handleBack)];
    
        self.navigationItem.leftBarButtonItem = backButton;
        // Do any additional setup after loading the view from its nib.
    }
    
    -(void)handleBack {
        [self.navigationController.navigationBar setUserInteractionEnabled:false];
        [self.navigationController popViewControllerAnimated:true];
    }
    

    That will do the job

    Edit 3:

    You need to set the User interaction of Navigation bar you can achieve the same effect by disabling the user interaction of left and right bar button items

        for item in self.navigationItem.leftBarButtonItems! {
            item.isEnabled = false
        }
    
        for item in self.navigationItem.rightBarButtonItems! {
            item.isEnabled = false
        }
    

    And you can enable and disable them as per your logic either in viewDidLoad,viewWillAppear or wherever it is appropriate to your code.

    Objective-C

    for(UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
        [item setEnabled:false];
    }
    
    for(UIBarButtonItem *item in self.navigationItem. rightBarButtonItems) {
        [item setEnabled:false];
    }