Skip to content

Instantly share code, notes, and snippets.

@MTattin
Last active September 5, 2020 13:17
Show Gist options
  • Save MTattin/b841389c95fafbbc92d0e761550e3856 to your computer and use it in GitHub Desktop.
Save MTattin/b841389c95fafbbc92d0e761550e3856 to your computer and use it in GitHub Desktop.
UINavigationControllerのデフォルトの戻るボタンを除去する ref: http://qiita.com/MTattin/items/2e43926869585e7011ae
///
/// CstmNV - 戻るボタン非表示
///
class CstmNC: UINavigationController {
///
// MARK: ------------------------------ life cycle
///
///
///
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
super.pushViewController(viewController, animated: animated)
///
/// デフォルトの戻るボタンを非表示
///
viewController.navigationItem.backBarButtonItem = nil
viewController.navigationItem.hidesBackButton = true
}
}
///
// MARK: ------------------------------ UINavigationBarDelegate
///
///
///
extension CstmNC: UINavigationBarDelegate {
///
///
///
func navigationBar(_ navigationBar: UINavigationBar, shouldPush item: UINavigationItem) -> Bool {
///
/// サイズを調整 - ここでitemに対して戻るボタンの非表示をしようとしても上手くいかなかったのでpushViewController側で実装
///
item.titleView?.frame = navigationBar.frame
return true
}
}
///
/// CstmNV - 戻るボタン非表示
///
class CstmNC: UINavigationController {
///
// MARK: ------------------------------ life cycle
///
///
///
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
///
/// デフォルトの戻るボタンを非表示
///
self.visibleViewController?.navigationItem.backBarButtonItem = nil
self.visibleViewController?.navigationItem.hidesBackButton = true
///
/// titleViewに制約設定
///
if #available(iOS 11.0, *) {
if let v: UIView = self.visibleViewController?.navigationItem.titleView {
v.translatesAutoresizingMaskIntoConstraints = false
///
///
vとv.superview?に対して制約設定
iOS10と合わせるなら上下0、左右8の制約を指定すればOK
///
///
v.superview?.layoutIfNeeded()
}
}
}
///
///
///
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
super.pushViewController(viewController, animated: animated)
///
/// デフォルトの戻るボタンを非表示
///
viewController.navigationItem.backBarButtonItem = nil
viewController.navigationItem.hidesBackButton = true
viewController.navigationItem.titleView?.frame = self.navigationBar.frame
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment