How to remove all navigationbar back button title

后端 未结 30 3292
后悔当初
后悔当初 2020-12-12 15:42

When I push a UIViewController, it has some title in back button at new UIViewController, if the title has a lot of text, It does not look good in

相关标签:
30条回答
  • 2020-12-12 16:13

    Swift 4.2 & 5

    Instead of playing with the navigation bar tint color which will have side effects if you are using image picker anytime later in your code.

    Use below code:

    extension UIViewController {
            open override func awakeFromNib() {
                navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        }  
    }
    

    Call it from your first ViewController:

    self.awakeFromNib()
    
    0 讨论(0)
  • 2020-12-12 16:14

    I'm using this line of code in AppDelegate file into didFinishLaunchingWithOptions method to remove the backbutton title.

    Swift 2.x

    let barAppearace = UIBarButtonItem.appearance()
    barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics:UIBarMetrics.Default)
    

    Swift 3.x

    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
    

    Swift 4.x

    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: UIControlState.highlighted)
    
    0 讨论(0)
  • 2020-12-12 16:16

    If you want back arrow so following code put into AppDelegate file into didFinishLaunchingWithOptions method.

    For Objective-C

     [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    

    For Swift

    let BarButtonItemAppearance = UIBarButtonItem.appearance()
    BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
    

    Another option give below.

    In Objective C

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    

    In Swift

    self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
    

    UPDATE :

        let BarButtonItemAppearance = UIBarButtonItem.appearance()
    
        let attributes: [NSAttributedStringKey: Any] = [
        BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
                NSAttributedStringKey.font: UIFont.systemFont(ofSize: 0.1),
                NSAttributedStringKey.foregroundColor: UIColor.clear]
    
        BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
        BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
    

    UPDATE SWIFT 4.1 :

        let attributes = [NSAttributedStringKey.font:  UIFont(name: "Helvetica-Bold", size: 0.1)!, NSAttributedStringKey.foregroundColor: UIColor.clear]
    
        BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
        BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
    

    Using Offset

    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, 0), for:UIBarMetrics.default)
    

    So may be your problem has been solve.

    Happy coding.

    0 讨论(0)
  • 2020-12-12 16:16

    I don't know why but I found problem with hiding back button title in iPhone pluses but in device without plus shows correct with

    leftBarButtonItem.title = ""
    

    So I found simple way. It is set tint color to clear in NavigationBar of NavigationViewController in autolayout. It may be problem if you use icons or text tiles with tint. But in my case I don't use it as all.

    0 讨论(0)
  • 2020-12-12 16:18

    Just copy this code in didFinishLaunchingWithOptions launchOptions

    Swift 5

    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -1000.0, vertical: 0.0), for: .default)
    

    Swift 4

    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000.0, 0.0), for: .default)
    
    0 讨论(0)
  • 2020-12-12 16:18
    let barAppearace = UIBarButtonItem.appearance()
    barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
    

    used this line of code in swift 3.0

    0 讨论(0)
提交回复
热议问题