默认后退按钮文本和字体设置

时间:2016-06-27 08:42:25

标签: ios swift2 uibarbuttonitem back-button navigationbar

默认情况下,导航后退按钮文本显示为上一个屏幕标题或<

我正在尝试将其更改为< = |

但它的到来如图所示 BackButton Image. 所以,我想知道如何更改其字体以使大< = |并删除默认的<

我试过

在第一个开始屏幕的viewDidLoad中尝试了相同的代码, 所以我也想知道在哪里放置这段代码:

override func viewWillAppear(animated: Bool)
{
    self.navigationItem.leftBarButtonItem?.title = "<=|"
    let FntStgVal = [NSFontAttributeName:UIFont.systemFontOfSize(50, weight: UIFontWeightLight)]
    self.navigationItem.leftBarButtonItem?.setTitleTextAttributes(FntStgVal, forState: .Normal)
}

1 个答案:

答案 0 :(得分:2)

viewDidLoad中更改您的代码。

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func setNavigationWithCustomBackButton() {
        let btnLeft:UIButton! = UIButton(frame: CGRectMake(0, 0, 20, 16))
        btnLeft.setTitle("<=|", forState: .Normal)
        btnLeft.titleLabel?.font = UIFont.systemFontOfSize(19, weight: UIFontWeightLight)
        btnLeft!.addTarget(self, action: "handleBack:",forControlEvents: UIControlEvents.TouchUpInside)
        let leftItem:UIBarButtonItem = UIBarButtonItem(customView: btnLeft!)
        self.navigationItem.leftBarButtonItem = leftItem
    }

    func handleBack(sender: UIButton) {
        self.navigationController?.popViewControllerAnimated(true)
    }
}

现在使用此BaseViewController作为您所有viewController的父级,并在此viewDidLoad中调用其方法。

class ViewController1: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNavigationWithCustomBackButton()
    }
}

现在它会在NavigationBar中添加自定义后退按钮。
希望这会对你有所帮助。