How to hide the “back” button in UINavigationController?

前端 未结 14 2371
盖世英雄少女心
盖世英雄少女心 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 08:04

    The solution suggest by Zoran Simic didn't work for me for some reason.

    This code did work however:

    MyController* controller   =   [[MyController alloc]  init];
    NSArray* array             =   [[[NSArray alloc] initWithObjects:controller, nil] autorelease];
    
    [self.navigationController setViewControllers:array animated:NO];
    
    [controller release];
    

    Obviously you'd have to manipulate an NSArray to your taste to make it work for you. Hope that helps somebody :)

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

    This hides the back button and replaces it with an add button in Swift:

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
    
        // This hides the back button while in editing mode, which makes room for an add item button
        self.navigationItem.setHidesBackButton(editing, animated: animated)
    
        if editing {
            // This adds the add item button
            let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
            // Use the animated setter for the left button so that add button fades in while the back button fades out
            self.navigationItem.setLeftBarButton(addButton, animated: animated)
            self.enableBackGesture(enabled: false)
        } else {
            // This removes the add item button
            self.navigationItem.setLeftBarButton(nil, animated: animated)
            self.enableBackGesture(enabled: true)
        }
    }
    
    func enableBackGesture(enabled: Bool) {
        // In addition to removing the back button and adding the add item button while in edit mode, the user can still exit to the previous screen with a left-to-right swipe gesture in iOS 7 and later. This code disables this action while in edit mode.
        if let navigationController = self.navigationController {
            if let interactivePopGestureRecognizer = navigationController.interactivePopGestureRecognizer {
                interactivePopGestureRecognizer.isEnabled = enabled
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 08:08

    In my UIViewController subclass I have this method:

    -(void)setEditing:(BOOL)editing animated:(BOOL)animated {
        [super setEditing:editing animated: animated];
    
        // hide back button in edit mode
        [self.navigationItem setHidesBackButton:editing animated:YES];
    }
    
    0 讨论(0)
  • 2020-12-04 08:10

    Swift iOS (I have used following)

    // hide back button
            self.navigationItem.setHidesBackButton(true, animated: false)
    
    // pgrm mark ----- ------
    
        // hide the back button for this view controller
    
        override func setEditing(editing: Bool, animated: Bool) {
            super.setEditing(editing, animated: animated)
    
            self.navigationItem.setHidesBackButton(editing, animated: animated)
    
        }// end setEditing
    
    0 讨论(0)
  • 2020-12-04 08:11

    I just found out the answer, in a controller use this:

    [self.navigationItem setHidesBackButton:YES animated:YES];
    

    And to restore it:

    [self.navigationItem setHidesBackButton:NO animated:YES];
    

    --

    [UPDATE]

    Swift 3.0:

    self.navigationItem.setHidesBackButton(true, animated:true)
    
    0 讨论(0)
  • 2020-12-04 08:13

    In my case I had few issues with current answers:

    • inside viewDidLoad/viewWillAppear only back icon was hidden and the string "Back" was inactive but still visible
    • inside viewDidAppear the back button disappeared...but I did not want the user to see it at all

    So the solution that finally have worked for me is:

    - (id)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
    
        if (self) {
            [self.navigationItem setHidesBackButton:YES animated:NO];
        }
    
        return self;
    }
    
    0 讨论(0)
提交回复
热议问题