适配刘海

const IPHONE_X_HEIGHT_WIDTH_RATIO = 1280 / 716;
const IPHONE_X_TOP_ADJUSTMENT = 0.037;

/**
 * 适配刘海
 * @param node
 */
export function applyAdapterFringe(node: cc.Node): void {
    if (!node) {
        return;
    }

    const widget = node.getComponent(cc.Widget);
    if (!widget) {
        return;
    }

    const size = cc.view.getFrameSize();
    const size2 = cc.view.getVisibleSize();

    function isFringe(size: cc.Size, size2: cc.Size): boolean {
        if (size.width / size.height > (750 + 10) / 1334) {
            return false;
        }

        if (cc.sys.os === cc.sys.OS_ANDROID || cc.sys.isBrowser || MACRO_IOS) {
            return (size.height / size.width > IPHONE_X_HEIGHT_WIDTH_RATIO) ||
                (size2.height / size2.width > IPHONE_X_HEIGHT_WIDTH_RATIO);
        }

        return false;
    }

    if (isFringe(size, size2)) {
        const height = Math.max(size.height, size2.height);
        widget.top += IPHONE_X_TOP_ADJUSTMENT * height;
    }
}

assignWindowSafe({ applyAdapterFringe });
posted @ 2026-01-29 18:01  程序猿Christmas  阅读(1)  评论(0)    收藏  举报