简体   繁体   English

iOS:将导航栏添加到应用程序窗口与状态栏发生冲突

[英]iOS: Adding navigation bar to Application window collides with status bar

I have added navigation bar into application window but navigation bar collides with status bar.我已将导航栏添加到应用程序窗口中,但导航栏与状态栏发生冲突。 (I need to do it with application window). (我需要用应用程序窗口来做)。

Code I've tried is:我试过的代码是:

UIWindow * firstWindow = [UIApplication sharedApplication].windows[0];

UINavigationBar * navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, firstWindow.frame.size.width, 64)];

/*
// I tried these also
UINavigationBar * navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, firstWindow.frame.size.width, 84)];

UINavigationBar * navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, firstWindow.frame.size.width, 64)];
*/

[firstWindow addSubview:navBar];

Here are results:以下是结果:

在此处输入图片说明

在此处输入图片说明

I looked around these and tried all answers related to default navigation bar but nothing works:我环顾四周并尝试了与默认导航栏相关的所有答案,但没有任何效果:

What is the height of Navigation Bar in iOS 7? iOS 7 中导航栏的高度是多少?

iOS 10 custom navigation bar height iOS 10 自定义导航栏高度

iOS 7 status and navigation bar different height in storyboard than in app iOS 7 状态和导航栏在情节提要中与应用程序中的高度不同

UINavigationBar/Status Bar issue in IOS7 IOS7 中的 UINavigationBar/Status Bar 问题

When hiding the statusbar my navigation bar moves up in iOS7 隐藏状态栏时,我的导航栏在 iOS7 中向上移动

iOS 7 Status Bar Collides With NavigationBar using ViewController iOS 7 状态栏与使用 ViewController 的 NavigationBar 发生冲突

为 NavigationController 使用 additionalSafeAreaInsets,

self.navigationController.additionalSafeAreaInsets = UIEdgeInsetsMake(20, 0, 0, 0);

This is how you would do it in iOS 11 using the safe area insets.这就是您在 iOS 11 中使用安全区域插图的方式。 You need a view controller under the hood so you can at least get notified when the safe area insets are updated.你需要一个引擎盖下的视图控制器,这样你至少可以在安全区域插入更新时得到通知。 This code is from the single app template in Xcode 9. You can see the frame update between portrait and landscape mode as the safe areas are updated.此代码来自 Xcode 9 中的单个应用程序模板。随着安全区域的更新,您可以看到纵向和横向模式之间的帧更新。

import UIKit

class ViewController: UIViewController {
    private var window: UIWindow!
    private var navbar: UINavigationBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.window = UIApplication.shared.windows[0]
        self.navbar = UINavigationBar(frame: CGRect(x: 0, y: self.view.safeAreaInsets.top, width: window.frame.size.width, height: 64))
        self.navbar.barTintColor = .red
        window.backgroundColor = .gray
        window.addSubview(navbar)
        self.view.backgroundColor = .clear
    }

    override func viewSafeAreaInsetsDidChange() {
        let navBarHeight: CGFloat = 64
        self.navbar.frame = CGRect(x: 0, y: self.view.safeAreaInsets.top, width: self.window.bounds.width, height: navBarHeight)
    }
}

Here is an image from the simulator:这是模拟器的图像:

在此处输入图片说明

If you need to support iOS 10 and below then you need to also use the topLayoutGuide which was deprecated.如果您需要支持 iOS 10 及更低版本,则还需要使用已弃用的 topLayoutGuide。 The length property will give you similar information as the safe layout guides . length属性将为您提供与安全布局指南类似的信息。

I feel compelled to add though that finding a way to use the navigation controller would be much easier and safer to use especially as you start supporting multiple versions of iOS and Apple upgrades UIKit.我觉得有必要补充一下,找到一种使用导航控制器的方法会更容易、更安全,尤其是当您开始支持多个版本的 iOS 和 Apple 升级 UIKit 时。

Try this试试这个

 CGFloat height =  [UIApplication sharedApplication].statusBarFrame.size.height;

 UIWindow * firstWindow = [UIApplication sharedApplication].windows[0];

 UINavigationBar * navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, height, firstWindow.frame.size.width, 64)];

 [firstWindow addSubview:navBar];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM