{"id":4808,"date":"2021-12-27T20:23:45","date_gmt":"2021-12-27T12:23:45","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4808"},"modified":"2022-01-26T20:05:43","modified_gmt":"2022-01-26T12:05:43","slug":"swiftui-geometryreader%e7%9a%84%e4%b8%80%e7%82%b9%e7%90%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4808","title":{"rendered":"SwiftUI GeometryReader\u7684\u4e00\u70b9\u7406\u89e3"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1ahttps:\/\/www.wwt.com\/article\/using-geometryreader-to-build-relative-layouts<\/p>\n\n\n\n<p>\u867d\u7136\u5927\u591a\u6570\u65f6\u5019\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5229\u7528\u5404\u79cdStack\u5e76<a href=\"https:\/\/www.wwt.com\/article\/from-old-to-new-how-to-use-existing-uikit-views-in-swiftui\">\u5141\u8bb8 SwiftUI \u8c03\u6574\u81ea\u5df1\u7684\u5e03\u5c40\uff0c<\/a>\u4f46\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u5bf9\u89c6\u56fe\u5728\u5c4f\u5e55\u4e0a\u7684\u663e\u793a\u65b9\u5f0f\u6709\u66f4\u591a\u7684\u63a7\u5236\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 GeometryReader \u6765\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u6807\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a Text \u89c6\u56fe\u5e76\u7ed9\u5b83\u4e00\u4e2a\u9ec4\u8272\u80cc\u666f\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u89c6\u56fe\u7684\u6846\u67b6\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSwiftUI \u4f1a\u5c06 Text \u653e\u7f6e\u5728\u4e2d\u5fc3\uff0c\u5e76\u4e14\u53ea\u5360\u7528\u89c6\u56fe\u6240\u9700\u7684\u7a7a\u95f4\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-267-1024x315.png\" alt=\"\" class=\"wp-image-4809\" width=\"394\" height=\"121\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-267-1024x315.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-267-300x92.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-267-768x236.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-267-830x255.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-267-230x71.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-267-350x108.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-267-480x148.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-267.png 1444w\" sizes=\"(max-width: 394px) 100vw, 394px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u89c6\u56fe\u5360\u636e\u6574\u4e2a\u5bbd\u5ea6\u800c\u4e0d\u7ba1\u5b83\u5305\u542b\u591a\u5c11\u6587\u672c\u600e\u4e48\u529e\uff1f\u8fd9\u5c31\u662f GeometryReader \u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\u3002<a rel=\"noreferrer noopener\" href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/geometryreader\" target=\"_blank\">\u6839\u636e Apple \u7684\u8bf4\u6cd5\uff0cGeometryReader \u662f\u201c\u4e00\u4e2a\u5bb9\u5668\u89c6\u56fe\uff0c\u5b83\u5c06\u5176\u5185\u5bb9\u5b9a\u4e49\u4e3a\u5176\u81ea\u8eab\u5927\u5c0f\u548c\u5750\u6807\u7a7a\u95f4\u7684\u51fd\u6570\u3002\u201d<\/a><\/p>\n\n\n\n<p>\u4e0e SwiftUI \u4e2d\u7684\u5927\u591a\u6570\u5185\u5bb9\u4e00\u6837\uff0cGeometryReader \u662f\u4e00\u4e2a\u89c6\u56fe\uff0c\u5728\u5b50\u89c6\u56fe\u4e2d\u53ef\u4ee5\u62ff\u5230\u7236\u89c6\u56fe\u5206\u914d\u7684\u6700\u5927\u5c3a\u5bf8\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6211\u4eec\u7684\u5b50\u89c6\u56fe\u8986\u76d6\u5c4f\u5e55\u5bbd\u5ea6\u7684\u4e00\u534a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b50\u89c6\u56fe\u653e\u5165 GeometryReader \u5e76\u5c06\u5176\u5bbd\u5ea6\u8bbe\u7f6e\u4e3a\u6700\u5927\u5206\u914d\u5bbd\u5ea6\u7684\u4e00\u534a\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-268-523x1024.png\" alt=\"\" class=\"wp-image-4812\" width=\"238\" height=\"465\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-268-523x1024.png 523w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-268-153x300.png 153w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-268-350x686.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-268-480x940.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-268.png 534w\" sizes=\"(max-width: 238px) 100vw, 238px\" \/><\/figure><\/div>\n\n\n\n<p>\u4f60\u4f1a\u6ce8\u610f\u5230\u5b50\u89c6\u56fe\u73b0\u5728\u662f\u5c4f\u5e55\u5bbd\u5ea6\u7684\u4e00\u534a\uff0c\u4f46\u5b83\u4e5f\u79fb\u52a8\u5230\u9876\u90e8\u524d\u7f18\u800c\u4e0d\u662f\u5c45\u4e2d\u3002GeometryReader \u8c03\u6574\u81ea\u8eab\u4ee5\u5360\u7528\u5c3d\u53ef\u80fd\u591a\u7684\u53ef\u7528\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p><strong>\u901a\u8fc7\u5c06 GeometryReader \u80cc\u666f\u989c\u8272\u8bbe\u7f6e\u4e3a\u84dd\u8272\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u770b\u5230\u8fd9\u4e00\u70b9\u3002GeometryReader \u7684\u84dd\u8272\u80cc\u666f\u8986\u76d6\u4e86\u5b89\u5168\u533a\u57df\u5185\u7684\u6240\u6709\u5185\u5bb9\uff08\u7236\u89c6\u56fe\u5c3d\u53ef\u80fd\u63d0\u4f9b\u5927\u7684\u7a7a\u95f4\u7ed9\u5b50\u89c6\u56fe\uff09\uff0c\u4f46\u5b50\u89c6\u56fe Text \u672c\u8eab\u662f GeometryReader \u533a\u57df\u5185\u7684\u4e00\u4e2a\u8f83\u5c0f\u7684\u89c6\u56fe\u3002<span class=\"has-inline-color has-vivid-red-color\">\u8bb0\u4f4f<meta charset=\"utf-8\">GeometryReader\u672c\u8eab\u4e5f\u662f\u4e00\u4e2aview\uff0c\u6240\u4ee5\u4ed6\u4f1a\u6709\u81ea\u5df1\u7684Frame\uff08width\uff0cheight\uff09\uff0c\u4f1a\u5360\u7528\u5e03\u5c40\u7a7a\u95f4\u3002<\/span><\/strong><\/p>\n\n\n\n<p>\u8fd9\u53ef\u80fd\u662f\u6240\u9700\u7684\u7ed3\u679c\uff0c\u4f46\u51fa\u4e8e\u793a\u4f8b\u76ee\u7684\uff0c\u6211\u4eec\u5e0c\u671b\u4fdd\u6301\u6587\u672c\u5c45\u4e2d\u3002\u6211\u4eec\u6709\u51e0\u4e2a\u9009\u9879\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u4e00\u4e2a\u662f\u5c06 Text \u5305\u88f9\u5728stack\u548cspace\u7684\u7ec4\u5408\u4e2d\uff0c\u6216\u8005\u8c03\u6574stack\u7684frame\u4ee5\u586b\u5145 GeometryReader \u5360\u7528\u7684\u5168\u90e8\u7a7a\u95f4\u3002\u4f46\u662f\u7531\u4e8e\u6211\u4eec\u5df2\u7ecf\u5728\u4f7f\u7528 GeometryReader\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u5173\u4e8e\u6211\u4eec\u5b50\u89c6\u56fe\u5728\u7236\u89c6\u56fe\u7684\u4fe1\u606f\u3002&nbsp;<\/p>\n\n\n\n<p>\u5728 GeometryReader \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528<strong>geometry.frame(in: .local)<\/strong>\u5230 (midX, midY)\u6dfb\u52a0\u4e00\u4e2a\u4f4d\u7f6e\u3002\u4f4d\u7f6e\u4f1a\u5c06\u89c6\u56fe\u7684\u4e2d\u5fc3\u653e\u5728\u5176\u7236\u7ea7\u5185\u7684\u6307\u5b9a\u5750\u6807\u5904\u3002<\/p>\n\n\n\n<p>GeometryReader \u7684\u4e00\u4e2a\u597d\u5904\u662f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u8c03\u6574\u4ee5\u586b\u5145\u6240\u6709\u53ef\u7528\u7a7a\u95f4\uff0c\u5b83\u7684\u5b50\u89c6\u56fe\u5c06\u6839\u636e GeometryReader \u672c\u8eab\u6709\u591a\u5c11\u5c4f\u5e55\u7a7a\u95f4\u8fdb\u884c\u8c03\u6574\u3002\u5982\u679c\u6211\u4eec\u5728 GeometryReader \u4e4b\u5916\u6dfb\u52a0\u4e00\u4e2a HStack \u5e76\u6dfb\u52a0\u4e00\u4e2a\u524d\u9762\u7684 Image \u89c6\u56fe\uff0c\u6211\u4eec\u7684 Text \u5c06\u51cf\u5c0f\u5176\u5bbd\u5ea6\u4ee5\u4fdd\u6301\u5176\u7236\u89c6\u56fe GeometryReader \u5bbd\u5ea6\u7684\u4e00\u534a\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-269-503x1024.png\" alt=\"\" class=\"wp-image-4813\" width=\"245\" height=\"499\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-269-503x1024.png 503w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-269-147x300.png 147w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-269-230x468.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-269-350x713.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-269-480x977.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-269.png 504w\" sizes=\"(max-width: 245px) 100vw, 245px\" \/><\/figure><\/div>\n\n\n\n<p>\u56e0\u4e3a GeometryReader \u662f\u4e00\u4e2a SwiftUI \u89c6\u56fe\uff0c\u5b83\u5c06\u6709\u5173\u5176\u5750\u6807\u7a7a\u95f4\u7684\u4fe1\u606f\u4f20\u9012\u7ed9\u5176\u5b50\u89c6\u56fe\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u76f8\u5bf9\u5b9a\u4f4d\u548c\u5927\u5c0f\u5e03\u5c40\u89c6\u56fe\u65f6\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u66f4\u591a\u63a7\u5236\u6743\u3002<\/p>\n\n\n\n<p><strong>\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5927\u5bb6\u53ef\u4ee5\u52a0\u6df1\u7406\u89e3\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct ContentView: View {\n    var body: some View {\n        VStack {\n            GeometryReader { geometry in\n                Text(\"A:\\(geometry.size.width)_\\(geometry.size.height)\")\n                    .padding()\n            }.background(Color.red)\n            GeometryReader { geometry in\n                Text(\"B:\\(geometry.size.width)_\\(geometry.size.height)\")\n                    .padding()\n            }.background(Color.blue)\n        }\n     \n    }\n}<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-40-562x1024.png\" alt=\"\" class=\"wp-image-4950\" width=\"311\" height=\"567\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-40-562x1024.png 562w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-40-165x300.png 165w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-40-230x419.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-40-350x638.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-40-480x874.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-40.png 706w\" sizes=\"(max-width: 311px) 100vw, 311px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct ContentView: View {\n    var body: some View {\n        VStack {\n            GeometryReader { geometry in\n                Text(\"A:\\(geometry.size.width)_\\(geometry.size.height)\")\n                    .padding()\n            }.background(Color.red)\n        }\n\n    }\n}<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-41-581x1024.png\" alt=\"\" class=\"wp-image-4951\" width=\"342\" height=\"603\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-41-581x1024.png 581w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-41-170x300.png 170w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-41-230x406.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-41-350x617.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-41-480x846.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/01\/\u56fe\u7247-41.png 744w\" sizes=\"(max-width: 342px) 100vw, 342px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/www.wwt.com\/article\/using-geometryreader-to- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4808"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4808"}],"version-history":[{"count":7,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4808\/revisions"}],"predecessor-version":[{"id":4953,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4808\/revisions\/4953"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}