LoginSignup
0
1

More than 1 year has passed since last update.

NavigationBarのBackbarButtonItemをカスタムする!!

Posted at

はじめに

画面遷移した際のNavigationBarのBackボタン、デフォルトだと< Backという文字が入っていますが、この文字をカスタム出来る事を知りましたので、紹介します。
尚、以下のアドレスのAppにコードを追記していきたいと思います。
https://qiita.com/masasumi0327/items/fe285b606ea99a3026ba

1.Backの文字を消したい!

コードを記述します。

ViewController
import UIKit

class ViewController: UIViewController {
    @IBAction func toSecondViewButton(_ sender: Any) {
        let secondVC = storyboard?.instantiateViewController(identifier: "secondView") as! SecondViewController
        navigationController?.pushViewController(secondVC,animated: true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let backBarButtonItem = UIBarButtonItem()
        backBarButtonItem.title = ""//SecondViewControllerのNavigationItemの文字列変換
        self.navigationItem.backBarButtonItem = backBarButtonItem
    }
}

ポイントは遷移前のViewControllerにコードを記述して下さい。

はい!消えましたね!

2.カスタムいろいろ

UIImage等も設定出来ますのでコードを記述していきます。

SecondViewController
import UIKit

class SecondViewController: UIViewController {
    
    @IBAction func toThirdViewButton(_ sender: Any) {
        let thirdVC = storyboard?.instantiateViewController(identifier: "thirdView") as! ThirdViewController
        navigationController?.pushViewController(thirdVC,animated: true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //backBarButtonの設定
        let backBarButtonItem = UIBarButtonItem(image:UIImage(systemName: "arrowshape.turn.up.backward.fill"), style: .done, target: self, action: #selector(toSecondView))
        
        self.navigationItem.backBarButtonItem = backBarButtonItem
        backBarButtonItem.tintColor = UIColor.systemRed
    }
    @objc func toSecondView() {
        self.navigationController?.popViewController(animated: true)
    }
}

こんな感じで設定出来ました!!

以上になります。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1