{"id":7748,"date":"2025-06-01T14:54:25","date_gmt":"2025-06-05T07:06:25","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7748"},"modified":"2025-06-05T14:54:25","modified_gmt":"2025-06-05T07:06:25","slug":"https-badgameshow-com-steven-517","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-517\/","title":{"rendered":"\u5728 SwiftUI \u4e2d\u5be6\u73fe\u641c\u7d22\u6b04\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, SwiftUI, \u641c\u7d22\u6b04, SwiftUI \u6559\u5b78, \u641c\u7d22\u529f\u80fd\"><\/p>\n<h1>\u5728 SwiftUI \u4e2d\u5be6\u73fe\u641c\u7d22\u6b04\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>SwiftUI \u662f Apple \u5728 WWDC 2019 \u63a8\u51fa\u7684\u65b0\u6846\u67b6\uff0c\u65e8\u5728\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u8de8\u5e73\u53f0\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u4f7f\u7528\u641c\u7d22\u6b04\u662f\u4e00\u500b\u5e38\u898b\u4e14\u91cd\u8981\u7684\u529f\u80fd\uff0c\u80fd\u5920\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u4e26\u4f7f\u61c9\u7528\u7a0b\u5f0f\u66f4\u6613\u65bc\u4f7f\u7528\u8005\u5c0b\u627e\u6240\u9700\u7684\u8cc7\u6599\u3002\u672c\u6587\u5c07\u70ba\u60a8\u4ecb\u7d39\u5982\u4f55\u5728 SwiftUI \u4e2d\u6709\u6548\u5730\u5be6\u73fe\u641c\u7d22\u6b04\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\u548c\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>\u5efa\u7acb SearchBar \u7d44\u4ef6<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b <strong>SearchBar<\/strong> \u7684 View\uff0c\u7136\u5f8c\u5c07\u5b83\u5d4c\u5165\u5230\u6211\u5011\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u4ee5\u4e0b\u662f\u5efa\u7acb\u641c\u7d22\u6b04\u7684\u57fa\u672c\u4ee3\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">struct SearchBar: View {\n    @Binding var text: String\n\n    var body: some View {\n        HStack {\n            TextField(\"Search ...\", text: $text)\n                .padding(7)\n                .padding(.horizontal, 25)\n                .background(Color(.systemGray6))\n                .cornerRadius(8)\n                .overlay(\n                    HStack {\n                        Image(systemName: \"magnifyingglass\")\n                            .foregroundColor(.gray)\n                            .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)\n                            .padding(.leading, 8)\n                    }\n                )\n        }\n    }\n}\n<\/code><\/pre>\n<h2>\u5728 ContentView \u4e2d\u4f7f\u7528 SearchBar<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u9019\u500b <strong>SearchBar<\/strong> \u7d44\u4ef6\u653e\u5165 <strong>ContentView<\/strong> \u4e2d\uff0c\u4e26\u5c07\u5b83\u8207\u6211\u5011\u7684\u8cc7\u6599\u9032\u884c\u7d81\u5b9a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">struct ContentView: View {\n    @State private var searchText = \"\"\n\n    var body: some View {\n        VStack {\n            SearchBar(text: <span class=\"katex math inline\">searchText)\n            List {\n                \/\/ \u5728\u6b64\u8655\u52a0\u5165\u60a8\u7684\u8cc7\u6599\n                ForEach(data.filter {\n                    self.searchText.isEmpty ? true :<\/span>0.name.localizedStandardContains(self.searchText)\n                }, id: \\.self) { item in\n                    Text(item.name) \/\/ \u5047\u8a2d\u8cc7\u6599\u6709 name \u5c6c\u6027\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>TextField \u4e0d\u66f4\u65b0\uff1a<\/strong>\u78ba\u4fdd\u60a8\u6b63\u78ba\u7d81\u5b9a\u4e86 @Binding \u5c6c\u6027\u3002<\/li>\n<li><strong>\u8cc7\u6599\u672a\u6b63\u78ba\u7be9\u9078\uff1a<\/strong>\u6aa2\u67e5 filter \u689d\u4ef6\u4ee5\u78ba\u4fdd\u5b83\u7b26\u5408\u60a8\u7684\u8cc7\u6599\u7d50\u69cb\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u641c\u7d22\u529f\u80fd\uff0c\u60a8\u9084\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u6b64\u641c\u7d22\u6b04\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u52a0\u5165\u641c\u7d22\u6b77\u53f2\u8a18\u9304\u7684\u529f\u80fd\u3002<\/li>\n<li>\u5728\u641c\u7d22\u7d50\u679c\u4e2d\u9ad8\u4eae\u986f\u793a\u95dc\u9375\u5b57\u3002<\/li>\n<li>\u5be6\u73fe\u6a21\u7cca\u5339\u914d\u4ee5\u63d0\u9ad8\u641c\u7d22\u7684\u9748\u6d3b\u6027\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\u5728-SwiftUI-\u4e2d\u4f7f\u7528\u641c\u7d22\u6b04_1676963172.965922.webp\" alt=\"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u641c\u7d22\u6b04\" title=\"SwiftUI \u641c\u7d22\u6b04\u3001SwiftUI \u641c\u5c0b\u529f\u80fd\u3001SwiftUI \u641c\u5c0b\u6b04\u3001SwiftUI \u641c\u7d22\u529f\u80fd\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 SwiftUI \u4e2d\u5be6\u73fe\u591a\u689d\u4ef6\u641c\u7d22\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728 filter \u689d\u4ef6\u4e2d\u6dfb\u52a0\u591a\u500b\u689d\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">ForEach(data.filter {\n    self.searchText.isEmpty ? true : <span class=\"katex math inline\">0.name.localizedStandardContains(self.searchText) ||<\/span>0.description.localizedStandardContains(self.searchText)\n}, id: \\.self) { item in\n    Text(item.name)\n}<\/code><\/pre>\n<h3>2. \u641c\u7d22\u6b04\u7684\u4f48\u5c40\u5982\u4f55\u512a\u5316\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 SwiftUI \u7684\u5e03\u5c40\u7cfb\u7d71\uff0c\u4f8b\u5982\u4f7f\u7528 <code>Spacer()<\/code> \u548c <code>padding()<\/code> \u65b9\u6cd5\u4f86\u8abf\u6574\u641c\u7d22\u6b04\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\uff0c\u4ee5\u78ba\u4fdd\u5b83\u5728\u4e0d\u540c\u5c3a\u5bf8\u7684\u87a2\u5e55\u4e0a\u986f\u793a\u826f\u597d\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728\u641c\u7d22\u7d50\u679c\u4e2d\u986f\u793a\u5716\u7247\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728 <code>List<\/code> \u4e2d\u70ba\u6bcf\u500b\u9805\u76ee\u6dfb\u52a0\u4e00\u500b <code>Image<\/code>\uff0c\u4ee5\u986f\u793a\u76f8\u95dc\u7684\u5716\u7247\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">List {\n    ForEach(data.filter {\n        self.searchText.isEmpty ? true : $0.name.localizedStandardContains(self.searchText)\n    }, id: \\.self) { item in\n        HStack {\n            Image(item.imageName) \/\/ \u5047\u8a2d\u6bcf\u500b\u9805\u76ee\u6709 imageName \u5c6c\u6027\n                .resizable()\n                .scaledToFit()\n                .frame(width: 50, height: 50)\n            Text(item.name)\n        }\n    }\n}<\/code><\/pre>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0c\u5728 SwiftUI \u4e2d\u5be6\u73fe\u641c\u7d22\u6b04\u662f\u4e00\u500b\u7c21\u55ae\u4e14\u6709\u6548\u7684\u529f\u80fd\uff0c\u53ea\u9700\u5c07 <strong>SearchBar<\/strong> \u52a0\u5165\u61c9\u7528\u4e26\u8207\u8cc7\u6599\u7d81\u5b9a\uff0c\u5373\u53ef\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728SwiftUI\u4e2d\u4f7f\u7528\u641c\u7d22\u6b04\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u641c\u7d22\u529f\u80fd\u52a0\u5165\u5230\u4ed6\u5011\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u6211\u5011\u5c07\u8a73\u7d30\u4ecb\u7d39SwiftUI\u4e2d\u7684\u641c\u7d22\u6b04\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u641c\u7d22\u8cc7\u6599\u3002<\/p>\n","protected":false},"author":1,"featured_media":7747,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7748","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\u5728-SwiftUI-\u4e2d\u4f7f\u7528\u641c\u7d22\u6b04_1676963172.965922.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-20Y","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=7748"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7748\/revisions"}],"predecessor-version":[{"id":13088,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7748\/revisions\/13088"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7747"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}