简体   繁体   中英

Customize the spacing and formatting of back icon and text in navigation bar

I have added a custom image for back button in the navigation bar. Here is the function for setting up my navigation bar.

func setUpNavBar() {
    self.navigationController?.navigationBar.isHidden = false
    self.navigationController?.navigationBar.tintColor = UIColor.black
    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "BackArrow")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "BackArrow")
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: nil, action: nil)   
}

The resulting nav bar with above settings is as shown below.

在此处输入图片说明

Next, I want to increase the spacing between the back icon and the text and also increase the text size (also make it bold). How can this be done?

To change the size of the text and make it bold set the title textAttributes:

navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 14)], for: .normal)

To edit the title offset you can use the following:

navigationItem.backBarButtonItem?.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -10, vertical: 0), for: .default)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM