iOS11之后導航欄默認層級發(fā)生改變,自定義的左右view默認放在了_UIButtonBarStackView里面,而左邊的UIButtonBarStackView默認距離導航欄左邊20間距,右邊的UIButtonBarStackView默認距離導航欄右邊20間距,所以可以遍歷找到這個約束修改為0,便能取消間隔。
image.png
還有一種方案是把自定義的按鈕放在一個父View里,讓View整體想左(或向右)便宜,iOS默認可以顯示超出父視圖的部分,但是默認超出父視圖的部分不能響應事件,所以點擊超出部分沒有反應,不推薦這種做法
iOS11之前是直接把自定義的視圖放在了UINavigationBar上, 層級如下圖:
可以通過創(chuàng)建占位空間(UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil))來調(diào)整位置
image.png
import UIKit
class NavigationController: UINavigationController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// 設置半透明
self.navigationBar.isTranslucent = true
// navigationBar背景顏色
self.navigationBar.barTintColor = UIColor.white
// 左右按鈕文本
self.navigationBar.tintColor = UIColor.rgb(51, 51, 51)
// 標題文本
self.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),
NSAttributedString.Key.foregroundColor: UIColor.rgb(51, 51, 51)]
// 返回手勢可用
self.interactivePopGestureRecognizer?.isEnabled = true
self.interactivePopGestureRecognizer?.delegate = self
// 背景
// self.navigationBar.setBackgroundImage(UIImage(), for: .default)
// 隱藏navigationBar分割線
self.navigationBar.shadowImage = UIImage()
// self.navigationBar.isHidden = true
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
if self.viewControllers.count > 0 {
viewController.hidesBottomBarWhenPushed = true
// 替換系統(tǒng)返回鍵
viewController.addNavigationBarLeftView(self.customLeftBackButton())
}
super.pushViewController(viewController, animated: animated)
}
// 跟控制器不響應返回手勢
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return self.viewControllers.count > 1
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// ios11調(diào)整navigationBar上按鈕的左右間距,默認左右各20
self.adjustNavigationBarItemsSpacing()
}
/// 自定義返回按鈕
private func customLeftBackButton() -> UIButton {
let backBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
let image = UIImage(named: "navigaitonbar_back_btn_normal")
backBtn.setImage(image, for: .normal)
backBtn.imageView?.contentMode = .scaleAspectFit
backBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -60, bottom: 0, right: 0)
backBtn.addTarget(self, action: #selector(backBtnClicked), for: .touchUpInside)
return backBtn
}
@objc private func backBtnClicked() {
self.popViewController(animated: true)
}
/// ios11調(diào)整navigationBar上按鈕的左右間距,默認左右各20
private func adjustNavigationBarItemsSpacing() {
if #available(iOS 11, *) {
for subview in self.navigationBar.subviews {
if subview.className.contains("UINavigationBarContentView"){
for constant in subview.constraints {
if constant.constant == 20 || constant.constant == -20 {
constant.constant = 0 }
}
}
}
}
}
}
extension UIViewController {
/// 給系統(tǒng)導航欄添加左邊View(刪除左邊20間隔)
///
/// - Parameter leftView: leftView
func addNavigationBarLeftView(_ leftView: UIView) {
let leftItem = UIBarButtonItem(customView: leftView)
if #available(iOS 11, *) {
self.navigationItem.leftBarButtonItems = [leftItem]
} else {
// 用于消除左邊空隙,要不然按鈕頂不到最左邊
let leftSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
leftSpace.width = -20
self.navigationItem.leftBarButtonItems = [leftSpace, leftItem]
}
}
/// 給系統(tǒng)導航欄添加右邊View(刪除右邊20間隔)
///
/// - Parameter rightView: rightView
func addNavigationBarRightView(_ rightView: UIView) {
let rightItem = UIBarButtonItem(customView: rightView)
if #available(iOS 11, *) {
self.navigationItem.rightBarButtonItems = [rightItem]
} else {
// 用于消除右邊空隙,要不然按鈕頂不到最右邊
let rightSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
rightSpace.width = -20
self.navigationItem.rightBarButtonItems = [rightSpace, rightItem]
}
}
}

