{"id":2677,"date":"2025-02-22T21:46:30","date_gmt":"2025-02-22T13:46:30","guid":{"rendered":"https:\/\/www.xfan.top\/?p=2677"},"modified":"2025-02-22T21:46:30","modified_gmt":"2025-02-22T13:46:30","slug":"ios-swiftui-%e6%b2%89%e6%b5%b8%e5%bc%8f%e7%8a%b6%e6%80%81%e6%a0%8f-%ef%bc%88%e9%80%8f%e6%98%8e%e7%8a%b6%e6%80%81%e6%a0%8f%ef%bc%89%e4%b8%8e%e7%8a%b6%e6%80%81%e6%a0%8f%e6%96%87%e5%ad%97%e8%ae%be","status":"publish","type":"post","link":"https:\/\/www.xfan.top\/index.php\/2025\/02\/22\/2677\/","title":{"rendered":"iOS SwiftUI \u6c89\u6d78\u5f0f\u72b6\u6001\u680f \uff08\u900f\u660e\u72b6\u6001\u680f\uff09\u4e0e\u72b6\u6001\u680f\u6587\u5b57\u8bbe\u7f6e\u65b9\u6cd5"},"content":{"rendered":"<p>\u5982\u679c\u60f3\u8981WKWebView\u5ffd\u7565\u5b89\u5168\u8fb9\u8ddd\uff0c\u9700\u8981\u5982\u4e0b\u7684\u989d\u5916\u8bbe\u7f6e\uff1a<\/p>\n<pre><code>struct WebView: UIViewRepresentable {\n\n    let webView = WKWebView(frame: .zero, configuration: makeWebviewCfg())\n\n    \/\/ \u63a7\u5236\u72b6\u6001\u680f\u6587\u5b57\u989c\u8272\u7684\u53d8\u91cf\n    @Binding var statusBarStyle: ColorScheme\n\n    func makeUIView(context: Context) -&gt; WKWebView {\n        webView.scrollView.contentInsetAdjustmentBehavior = .never \/\/ \u5fc5\u987b\u52a0\u8fd9\u4e2a\u914d\u7f6e\uff0c\u5426\u5219WKWebView\u4e0d\u4f1a\u5ef6\u4f38\u5230\u72b6\u6001\u680f\u533a\u57df\n\n    class Coordinator: NSObject, ..., WKScriptMessageHandler,...{\n        \/\/ \u5728Coordinator\u4fee\u6539statusBarStyle\uff0c\u72b6\u6001\u680f\u6587\u5b57\u989c\u8272\u7acb\u523b\u53d8\u5316\n        self.parent.statusBarStyle = .light\n        self.parent.statusBarStyle = .dark\n    }<\/code><\/pre>\n<p>info.plist\u4e2d\uff0c\u52a0\u5165\u5982\u4e0b\u8bbe\u7f6e<\/p>\n<pre><code>    &lt;key&gt;UIViewControllerBasedStatusBarAppearance&lt;\/key&gt;\n    &lt;true\/&gt;\n\n    &lt;key&gt;UIStatusBarStyle&lt;\/key&gt;\n    &lt;string&gt;UIStatusBarStyleLightContent&lt;\/string&gt;<\/code><\/pre>\n<p>\u89c6\u56fe\u4e2d<\/p>\n<pre><code>struct ContentView: View {\n\n    \/\/ \u72b6\u6001\u680f\u6837\u5f0f\n    @State private var statusBarStyle: ColorScheme = .light\n    ...\n    var body: some View {\n        ...\n        GeometryReader { geometry in\n            ZStack {\n                ...\n                webView\n                    .ignoresSafeArea(edges: .top)  \/\/ webview\u5ffd\u7565\u9876\u90e8\u5b89\u5168\u533a\u57df\n                }\n                .ignoresSafeArea(edges: .all) \/\/ \u5ffd\u7565\u5b89\u5168\u8fb9\u8ddd\u8bbe\u4e3a.all\n                .statusBar(hidden: false) \/\/ \u663e\u793a\u72b6\u6001\u680f .true\u5373\u53ef\u9690\u85cf\u72b6\u6001\u680f\n                .preferredColorScheme(statusBarStyle) \/\/ \u63a7\u5236\u72b6\u6001\u680f\u6587\u5b57\u989c\u8272\uff08.light \u4e3a\u6df1\u8272\u6587\u5b57\uff0c.dark \u4e3a\u6d45\u8272\u6587\u5b57\uff09\n    ... \n    }\n    .onAppear{\n        self.webView = WebView(\n            ...,\n            statusBarStyle: $statusBarStyle, \/\/ \u7ed1\u5b9a\u63a7\u5236\u72b6\u6001\u680f\u4e3b\u9898\u7684\u53d8\u91cf\n            ...\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u60f3\u8981WKWebView\u5ffd\u7565\u5b89\u5168\u8fb9\u8ddd\uff0c\u9700\u8981\u5982\u4e0b\u7684\u989d\u5916\u8bbe\u7f6e\uff1a struct WebView: UIViewRe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2677","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/comments?post=2677"}],"version-history":[{"count":2,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2677\/revisions"}],"predecessor-version":[{"id":2679,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2677\/revisions\/2679"}],"wp:attachment":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/media?parent=2677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/categories?post=2677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/tags?post=2677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}