Remove text from Back button keeping the icon

前端 未结 29 2340
栀梦
栀梦 2020-11-28 23:26

I want to remove the text from the back button, but I want to keep the icon. I have tried

let backButton = UIBarButtonItem(title: \"\", style: UIBarButtonIt         


        
相关标签:
29条回答
  • 2020-11-29 00:04
    let button: UIButton = UIButton (type: UIButtonType.Custom)
    button.setImage(UIImage(named: "imageName"), forState: UIControlState.Normal)
    button.addTarget(self, action: "backButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    button.frame = CGRectMake(0, 0, 30, 30)
    let barButton = UIBarButtonItem(customView: button)
    
    self.navigationItem.leftBarButtonItem = barButton
    
    func backButtonPressed(btn : UIButton) {
    
        // Your code
    }
    
    0 讨论(0)
  • 2020-11-29 00:07

    If you have a ViewControllerA and you want to navigate to the ViewControllerB, in the ViewControllerA, you should set the current navigationItem with a new UIBarButtonItem and the title to a string with blank space before push to another view controller:

    Set the title to a string with a blank space, you can't set nil or "" (empty string) because the default value is nil

    let backItem = UIBarButtonItem()
    backItem.title = " "
    navigationItem.backBarButtonItem = backItem
    let controllerB = ViewControllerB()
    navigationController?.pushViewController(controllerB, animated: true)
    
    0 讨论(0)
  • 2020-11-29 00:08

    For the case where we have not control at all of the previous view controller (i.e. if we are working in a framework), we can delete the title of the back button as follow:

    // For iOS 10
    navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = String()
    
    // For iOS 11
    navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = nil
    

    What it does is to navigate to the last item of the navigation's stack and delete its back title. Make sure to save the original one when our view controller will appear:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        originalBackButtonTitle = navigationController?.navigationBar.items?.last?.backBarButtonItem?.title
        // Delete title somewhere here...
    }
    

    and then reassign it, in order to not disrupt any part of the app:

    override func viewWillDisappear(_ animated: Bool) {
        navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = originalBackButtonTitle
        super.viewWillDisappear(animated)
    }
    
    0 讨论(0)
  • 2020-11-29 00:10

    The text of the back button depends on the title of the master view.

    The trick is to clear the title if the master view disappears and set it again if it is shown again:

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        // needed to clear the text in the back navigation:
        self.navigationItem.title = " "
    }
    
    override func viewWillAppear(_ animated: Bool) {
       
        super.viewWillAppear(animated)
        self.navigationItem.title = "My Title"
    }
    
    0 讨论(0)
  • 2020-11-29 00:11

    Finally found perfect solution.

    Just add one transparent Image and add following code in your AppDelegate.

    UIBarButtonItem.appearance().setBackButtonBackgroundImage(#imageLiteral(resourceName: "transparent"), for: .normal, barMetrics: .default)
    
    0 讨论(0)
  • 2020-11-29 00:12

    If you wanted to remove the title of a back button from a pushed view controller, let's say <Settings to < in subSettingsViewController then you've to set backBarButtonItem title in SettingsViewController's viewWillDisappear() method.

    Objective-C:

    - (void)viewWillDisappear:(BOOL)animated
        [super viewWillDisappear:animated];
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
    }
    

    Swift:

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }
    
    0 讨论(0)
提交回复
热议问题