How to hide the “back” button in UINavigationController?

前端 未结 14 2370
盖世英雄少女心
盖世英雄少女心 2020-12-04 07:09

Do you know how to hide the \'back\' button in a UINavigationController? Also, how to show it back, but I guess that\'s very similar to hiding it...

Just like the ma

相关标签:
14条回答
  • 2020-12-04 07:55

    Just to clarify existing answers: the hidesBackButton property is the right answer, but it isn't clear in many answers what self refers to. Basically you should set self.navigationItem.hidesBackButton = YES in the view controller that is about to get pushed (or just got pushed) onto the UINavigationController.

    In other words, say I have a UINavigationController named myNavController. I want to put a new view on it, and when I do I don't want the back button to show anymore. I could do something like:

    UIViewController *newVC = [[UIViewController alloc] init];
    //presumably would do some stuff here to set up the new view controller
    newVC.navigationItem.hidesBackButton = YES;
    [myNavController pushViewController:newVC animated:YES];
    

    When the code finishes, the view controlled by newVC should now be showing, and no back button should be visible.

    0 讨论(0)
  • 2020-12-04 07:56

    sethidesbackbutton did not work for me for some reason

    I used this way ->

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 30)]] ;
    
    0 讨论(0)
  • 2020-12-04 07:57

    In addition to removing the back button (using the methods already recommended), don't forget the user can still 'pop' to the previous screen with a left-to-right swipe gesture in iOS 7 and later.

    To disable that (when appropriate), implement the following (in viewDidLoad for example):

     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
         self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    0 讨论(0)
  • 2020-12-04 07:57

    For hiding and showing the Back button conditionally you can use following code:

    -(void)viewDidAppear:(BOOL)animated
    {
        if ([tempAry count]==0)
        {
            [self.navigationItem setHidesBackButton:YES animated:YES];
        }
        else
        {
            [self.navigationItem setHidesBackButton:NO animated:YES];
        }
        [super viewDidAppear:animated];
    } 
    

    Note: in some cases, you have to put it in viewDidAppear method instead of viewWillAppear such cases like: when you are updating array of next class into previous class and then checking condition into next class as above.

    0 讨论(0)
  • 2020-12-04 08:01

    Swift 3.

    Generally, you should use Apple's per-ViewController API as described many times already on this page, but sometimes you need immediate control of the Back button.

    The following code hides the Back button and ensures that tap collision detection doesn't occur in the hidden button region.

    let emptyView = UIView(frame: .zero)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: emptyView)
    
    0 讨论(0)
  • 2020-12-04 08:01

    This hides the back button

    let backBtn = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)
    
    
    navigationItem.leftBarButtonItem = backBtn
    
    0 讨论(0)
提交回复
热议问题