如何适应ios13状态栏?

时间:2019-07-01 09:15:16

标签: ios objective-c ios13

当我在ios13 xcode11 beta上运行项目时。

[UIApplication sharedApplication].statusBarFrame.size.height

代码返回0。

我应该怎么做才能使其适应ios13?

4 个答案:

答案 0 :(得分:0)

使用UIStatusBarManager获取iOS13中的statusBar高度:

UIStatusBarManager *manager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;

CGFloat height = manager.statusBarFrame.size.height;

答案 1 :(得分:0)

SceneDelegate.swift文件

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let windowScene = (scene as? UIWindowScene) else { return }
    if let statusBarFrame = windowScene.statusBarManager?.statusBarFrame {
        print(statusBarFrame)
    }
}

答案 2 :(得分:0)

正如 Peter Tamarous的答案中指出的那样,不赞成使用 keyWindow 属性,但是假设您使用的是单个窗口,则可以或者使用:

var images = [],
    bg_images = [],
    image_parents = [];
document.addEventListener('DOMContentLoaded', function () {
    var body = document.body;
    var elements = document.body.getElementsByTagName("*");

    /* When the DOM is ready find all the images and background images
        initially loaded */
    Array.prototype.forEach.call( elements, function ( el ) {
        var style = window.getComputedStyle( el, false );
        if ( el.tagName === "IMG" ) {
            images.push( el.src ); // save image src
            image_parents.push( el.parentNode ); // save image parent

        } else if ( style.backgroundImage != "none" ) {
            bg_images.push( style.backgroundImage.slice( 4, -1 ).replace(/['"]/g, "") // save background image url
        }
    }

    /* MutationObserver callback to add images when the body changes */
    var callback = function( mutationsList, observer ){
        for( var mutation of mutationsList ) {
            if ( mutation.type == 'childList' ) {
                Array.prototype.forEach.call( mutation.target.children, function ( child ) {
                    var style = child.currentStyle || window.getComputedStyle(child, false);
                    if ( child.tagName === "IMG" ) {
                        images.push( child.src ); // save image src
                        image_parents.push( child.parentNode ); // save image parent
                    } else if ( style.backgroundImage != "none" ) {
                        bg_images.push( style.backgroundImage.slice( 4, -1 ).replace(/['"]/g, "") // save background image url
                    }
                } );
            }
        }
    }
    var observer = new MutationObserver( callback );
    var config = { characterData: true,
                attributes: false,
                childList: true,
                subtree: true };

    observer.observe( body, config );
});

如果您需要访问 nth 窗口,则可以通过类似的方式进行操作:

CGSize statusBarSize = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size

答案 3 :(得分:0)

解决方案:

let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
lazy var statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0

说明:

您不能在window中使用let,因为创建该属性时该属性不存在,因为它属于自己。因此,在initself尚未完成。但是,如果您使用lazy var,那么self及其属性window将在您需要时准备就绪。

或其他解决方案使用singleton

struct AppConstants {
    static let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    static let statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
}