简体   繁体   中英

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 10 custom navigation bar height

iOS 7 status and navigation bar different height in storyboard than in app

UINavigationBar/Status Bar issue in IOS7

When hiding the statusbar my navigation bar moves up in iOS7

iOS 7 Status Bar Collides With NavigationBar using ViewController

为 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. 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.

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. The length property will give you similar information as the safe layout guides .

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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