iOS SwiftUI 沉浸式状态栏 (透明状态栏)与状态栏文字设置方法

如果想要WKWebView忽略安全边距,需要如下的额外设置:

struct WebView: UIViewRepresentable {

    let webView = WKWebView(frame: .zero, configuration: makeWebviewCfg())

    // 控制状态栏文字颜色的变量
    @Binding var statusBarStyle: ColorScheme

    func makeUIView(context: Context) -> WKWebView {
        webView.scrollView.contentInsetAdjustmentBehavior = .never // 必须加这个配置,否则WKWebView不会延伸到状态栏区域

    class Coordinator: NSObject, ..., WKScriptMessageHandler,...{
        // 在Coordinator修改statusBarStyle,状态栏文字颜色立刻变化
        self.parent.statusBarStyle = .light
        self.parent.statusBarStyle = .dark
    }

info.plist中,加入如下设置

    <key>UIViewControllerBasedStatusBarAppearance</key>
    <true/>

    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>

视图中

struct ContentView: View {

    // 状态栏样式
    @State private var statusBarStyle: ColorScheme = .light
    ...
    var body: some View {
        ...
        GeometryReader { geometry in
            ZStack {
                ...
                webView
                    .ignoresSafeArea(edges: .top)  // webview忽略顶部安全区域
                }
                .ignoresSafeArea(edges: .all) // 忽略安全边距设为.all
                .statusBar(hidden: false) // 显示状态栏 .true即可隐藏状态栏
                .preferredColorScheme(statusBarStyle) // 控制状态栏文字颜色(.light 为深色文字,.dark 为浅色文字)
    ... 
    }
    .onAppear{
        self.webView = WebView(
            ...,
            statusBarStyle: $statusBarStyle, // 绑定控制状态栏主题的变量
            ...
    }
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇