Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

ios - Default Back Button Text and Font Setting

By default, Navigation back button text comes as previous screen title or <

I am trying to change that to just <=|

But Its coming as shown in the picture BackButton Image. So, I want to know how to change its font to make big <=| and remove the default <

I tried

Tried the same code in viewDidLoad of first start screen, So i also want to know where to place this code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your code in viewDidLoad like this.

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)
    }
}

Now use this BaseViewController as parent of your all viewController and call its method in viewDidLoad like this.

class ViewController1: BaseViewController {

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

Now it will add custom back button in your NavigationBar.
Hope this will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...