iPhone Mini 中的导航栏间隙问题

Posted

技术标签:

【中文标题】iPhone Mini 中的导航栏间隙问题【英文标题】:Navigation bar gap issue in iPhone Mini 【发布时间】:2021-08-20 02:15:14 【问题描述】:

iPhone mini 中的导航栏间隙问题

仅在 iPhoneMini 中出现此问题。

如何解决 iPhone mini 中的这个差距?

var window: UIWindow?
var mainNavController = UINavigationController()

static var shared: SceneDelegate 
    let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
    let sceneDelegate = windowScene?.delegate as! SceneDelegate
    return sceneDelegate


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) 
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    IQKeyboardManager.shared.enable = true
    navigateToHome()
    guard let _ = (scene as? UIWindowScene) else  return 


func navigateToHome()
    let dashboardController = UIStoryboard.identifier(.Dashboard).instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
    mainNavController = UINavigationController(rootViewController: dashboardController)
    mainNavController.navigationBar.isHidden = true
    window?.rootViewController = mainNavController
    window?.makeKeyAndVisible()

// 在 HomeViewController 处

override func viewDidLoad() 
    super.viewDidLoad()
    setupUI()

    func setupUI()
        setTitle(text: "Home")
        setNavigationBar()

    func setNavigationBar(color: UIColor = .appThemeColor)
        navigationController?.navigationBar.backgroundColor = color
        navigationController?.isNavigationBarHidden = false
        navigationController?.navigationBar.hide_NavigationBar_BottonLine()
        navigationController?.statusBar(colour: color)
    

    func hide_NavigationBar_BottonLine()
        self.setBackgroundImage(UIImage(), for:.default)
        self.shadowImage = UIImage()
        self.layoutIfNeeded()
    

【问题讨论】:

你能显示你当前的代码吗?或者更好的是minimal reproducible example @aheze 已更新为当前代码 【参考方案1】:

与状态栏高度相关的问题

下面的扩展将解决这个问题。

extension UINavigationController 
    func statusBar(colour color : UIColor) 
        let statusBarFrame: CGRect
        if #available(ios 13.0, *) 
            statusBarFrame = view.window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero
         else 
            statusBarFrame = UIApplication.shared.statusBarFrame
        

        // iPhone Mini Status Bar height Issue fixed
        let keyWindow = UIApplication.shared.connectedScenes
            .filter($0.activationState == .foregroundActive)
            .map($0 as? UIWindowScene)
            .compactMap($0)
            .first?.windows
            .filter($0.isKeyWindow).first
        let height = UIApplication.shared.statusBarHeight
        let topInset: CGFloat = keyWindow?.safeAreaInsets.top ?? height
        let statusBarView = UIView(frame: statusBarFrame)
        statusBarView.frame.size.height = topInset
        statusBarView.backgroundColor = color
        view.addSubview(statusBarView)
    



// Status Bar Height
extension UIApplication 
    var statusBarHeight: CGFloat 
        connectedScenes
            .compactMap 
                $0 as? UIWindowScene
            
            .compactMap 
                $0.statusBarManager
            
            .map 
                $0.statusBarFrame
            
            .map(\.height)
            .max() ?? 0
    

【讨论】:

以上是关于iPhone Mini 中的导航栏间隙问题的主要内容,如果未能解决你的问题,请参考以下文章

如何减少iOS导航栏自定义视图的左右间隙

在基于导航的应用程序-iphone中的选定视图中隐藏导航栏

iPhone 7 中没有出现导航栏

为啥我的标志和导航栏之间有巨大的可点击间隙以及如何摆脱它?

iPhone 导航栏字体

如何使导航栏像 iPhone 中的照片应用一样透明并淡出