How to customize the navigation back symbol and navigation back text?

末鹿安然 提交于 2019-12-17 07:42:06

问题


This is the back icon and back text now:

But if I want my navigation back like this:

I have tried to set the back to my want icon image:

But it useless.


回答1:


You can hide back button text in many ways.Try this simple approach.

Step1: Goto your mainstoryBoard and click navigationBar.

Step 2: Goto Attributes Inspector under Navigation Item add a BLANK SPACE in Back Button

Step 3: If you want to change backButton text method is pretty much the same.

Update 1: If you want to use an image as a back button check this link


Update 2:

Method 2: Using custom image as a back button.

Paste below code into your detailVC and set image for your back Button.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

   title = "Detail VC"

    let customButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(backButtonTapped)) //
    self.navigationItem.leftBarButtonItem  = customButton  
}

func backButtonTapped() {     
   _ = navigationController?.popToRootViewController(animated: true)  
}

I am setting back button image in assets catalogue with the 32pixel size.I am not sure about the asset image size.Check with apple doc about the size class.

Output:




回答2:


Create a new UIBarButton and add it the navigationItem.leftBarButton.

let backButton = UIBarButtonItem(image: UIImage(named:"yourImage"), style: .plain, target: self, action: #selector(yourBackMethod(sender:))
navigationItem.leftBarButtonItem = = backButton


@objc internal func yourBackMethod(sender: AnyObject) {
  navigationController.popViewController()
}

Hope this helps.



来源:https://stackoverflow.com/questions/43546132/how-to-customize-the-navigation-back-symbol-and-navigation-back-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!