简体   繁体   English

自定义导航栏中返回图标和文本的间距和格式

[英]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:要更改文本的大小并使其加粗,请设置标题 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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