Last active
September 5, 2020 13:17
-
-
Save MTattin/b841389c95fafbbc92d0e761550e3856 to your computer and use it in GitHub Desktop.
UINavigationControllerのデフォルトの戻るボタンを除去する ref: http://qiita.com/MTattin/items/2e43926869585e7011ae
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// 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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// 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