{"id":190882,"date":"2023-09-18T10:44:11","date_gmt":"2023-09-18T02:44:11","guid":{"rendered":"https:\/\/www.idc.net\/help\/190882\/"},"modified":"2023-09-18T10:44:11","modified_gmt":"2023-09-18T02:44:11","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91ios%e6%95%99%e7%a8%8b%ef%bc%9a%e5%88%9b%e5%bb%ba%e9%ab%98%e6%80%a7%e8%83%bd%e7%9a%84%e5%8f%af%e6%bb%9a%e5%8a%a8-stack","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/190882\/","title":{"rendered":"\u540e\u6d6a\u4e91IOS\u6559\u7a0b\uff1a\u521b\u5efa\u9ad8\u6027\u80fd\u7684\u53ef\u6eda\u52a8 Stack"},"content":{"rendered":"<h2> \u6982\u89c8 <\/h2>\n<p> \u4f60\u7684 App \u9700\u8981\u5728\u5bb9\u5668\u89c6\u56fe\u4e2d\u663e\u793a\u7684\u6570\u636e\u7ecf\u5e38\u4f1a\u8d85\u8fc7\u8bbe\u5907\u5c4f\u5e55\u4e0a\u5bb9\u8bb8\u7684\u7a7a\u95f4\u3002\u5bf9\u4e8e\u91cd\u590d\u89c6\u56fe\u6216\u89c6\u56fe\u7ec4\uff0c\u6c34\u5e73 Stack \u548c\u5782\u76f4 Stack \u662f\u5f88\u597d\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u5b83\u4eec\u6ca1\u6709\u5185\u5efa\u7684\u6eda\u52a8\u673a\u5236\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c06 Stack \u5305\u88c5\u5728 <code>ScrollView<\/code> (\u82f1\u6587) \u4e2d\u6765\u6dfb\u52a0\u6eda\u52a8\uff0c\u5e76\u5728\u51fa\u73b0\u6027\u80fd\u95ee\u9898\u65f6\u5207\u6362\u5230\u60f0\u6027 Stack\u3002 <\/p>\n<h3> \u5728\u53ef\u6eda\u52a8\u7684\u5bb9\u5668\u4e2d\u663e\u793a\u89c6\u56fe\u7ec4 <\/h3>\n<p> \u5b9e\u65bd\u91cd\u590d\u89c6\u56fe\u6216\u89c6\u56fe\u7ec4\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u5b83\u4eec\u5305\u88c5\u5728 <code>ScrollView<\/code> (\u82f1\u6587) \u5185\u7684 <code>HStack<\/code> (\u82f1\u6587) \u6216 <code>VStack<\/code> (\u82f1\u6587) \u4e2d\u5373\u53ef\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>ScrollView<\/span>(.horizontal) {<\/span><\/span><span><span><\/span> <span> <span>HStack<\/span> {<\/span><\/span><span><span><\/span> <span> <span>ProfileView<\/span>()<\/span><\/span><span><span><\/span> <span> <span>ProfileView<\/span>()<\/span><\/span><span><span><\/span> <span> <span>ProfileView<\/span>()<\/span><\/span><span><span><\/span> <span> <span>ProfileView<\/span>()<\/span><\/span><span><span><\/span> <span> <span>ProfileView<\/span>()<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span>}<\/span><\/span><span><span><\/span> <span>.frame(maxWidth: <span>500<\/span>)<\/span><\/span><\/code>\n<\/pre>\n<p> \u5982\u679c\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\u7684 <code>ProfileView<\/code> \u5177\u6709 200 x 200 \u70b9\u7684\u56fa\u6709\u5185\u5bb9\u5927\u5c0f\uff0c\u5219 <code>frame(minWidth:idealWidth:maxWidth:minHeight:idealHeight:maxHeight:alignment:)<\/code> (\u82f1\u6587) \u89c6\u56fe\u4fee\u9970\u7b26\u5c06 500 \u70b9\u7684\u6700\u5927\u5bbd\u5ea6\u5e94\u7528\u4e8e <code>ScrollView<\/code> (\u82f1\u6587) \u4f1a\u5bfc\u81f4 Stack \u5728\u5176\u5185\u90e8\u6eda\u52a8\u3002 <\/p>\n<\/p>\n<p> \u6709\u5173\u5982\u4f55\u4f7f\u7528 Stack \u5c06\u89c6\u56fe\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u4ecb\u7ecd\uff0c\u8bf7\u53c2\u9605\u201c\u4f7f\u7528 Stack \u89c6\u56fe\u6784\u5efa\u5e03\u5c40\u201d\u3002 <\/p>\n<h3> \u4e3a\u4f60\u7684\u6570\u636e\u521b\u5efa\u91cd\u590d\u89c6\u56fe <\/h3>\n<p> \u4f7f\u7528 <code>ForEach<\/code> (\u82f1\u6587) \u6765\u4e3a\u4f60 App \u4e2d\u7684\u6570\u636e\u521b\u5efa\u91cd\u590d\u89c6\u56fe\u3002\u4ece <code>profiles<\/code> \u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u4e2a\u4eba\u8d44\u6599\u6570\u636e\u5217\u8868\u4e2d\uff0c\u5728 <code>HStack<\/code> (\u82f1\u6587) \u5185\u4f7f\u7528 <code>ForEach<\/code> (\u82f1\u6587) \u4e3a\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u521b\u5efa\u4e00\u4e2a <code>ProfileView<\/code>\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>ScrollView<\/span>(.horizontal) {<\/span><\/span><span><span><\/span> <span> <span>HStack<\/span> {<\/span><\/span><span><span><\/span> <span> <span>ForEach<\/span>(profiles) { profile <span>in<\/span><\/span><\/span><span><span><\/span> <span> <span>ProfileView<\/span>(profile: profile)<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span>}<\/span><\/span><span><span><\/span> <span>.frame(maxWidth: <span>500<\/span>)<\/span><\/span><\/code>\n<\/pre>\n<p> \u6ce8\u91ca <\/p>\n<p> \u5f53\u4f60\u4f7f\u7528 <code>ForEach<\/code> (\u82f1\u6587) \u65f6\uff0c\u4f60\u8fed\u4ee3\u7684\u6bcf\u4e2a\u5143\u7d20\u5fc5\u987b\u7ecf\u8fc7\u552f\u4e00\u6807\u8bc6\u3002\u53ef\u4ee5\u4f7f\u5143\u7d20\u9075\u4ece <code>Identifiable<\/code> (\u82f1\u6587) \u534f\u8bae\uff0c\u6216\u8005\u5c06\u4e00\u4e2a\u952e\u8def\u5f84\u4f20\u9012\u5230\u552f\u4e00\u7684\u6807\u8bc6\u7b26\u4f5c\u4e3a <code>init(_:id:content:)<\/code> (\u82f1\u6587) \u7684 <code>id<\/code> \u53c2\u6570\u3002 <\/p>\n<h3> \u8003\u8651\u5bf9\u5927\u91cf\u89c6\u56fe\u4f7f\u7528\u60f0\u6027\u5806\u53e0 <\/h3>\n<p> <code>HStack<\/code> (\u82f1\u6587)\u3001<code>VStack<\/code> (\u82f1\u6587) \u548c <code>ZStack<\/code> (\u82f1\u6587) \u4e09\u4e2a\u6807\u51c6 Stack \u89c6\u56fe\u5728\u663e\u793a\u65f6\u90fd\u4f1a\u8f7d\u5165\u5b83\u4eec\u7684\u6240\u542b\u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\uff0c\u800c\u4e00\u6b21\u6027\u8f7d\u5165\u5927\u91cf\u89c6\u56fe\u4f1a\u5bfc\u81f4\u8fd0\u884c\u65f6\u6027\u80fd\u53d8\u6162\u3002 <\/p>\n<p> \u5728\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0c<code>ProfileView<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u5d4c\u5957 Stack \u89c6\u56fe\u3001\u6587\u672c\u6807\u7b7e\u548c\u56fe\u50cf\u89c6\u56fe\u7684\u590d\u5408\u89c6\u56fe\u3002\u4e00\u6b21\u6027\u8f7d\u5165\u5927\u91cf\u4e2a\u4eba\u8d44\u6599\u4f1a\u5bfc\u81f4\u660e\u663e\u7684\u901f\u5ea6\u53d8\u6162\u3002 <\/p>\n<p> \u968f\u7740 Stack \u4e2d\u7684\u89c6\u56fe\u6570\u91cf\u589e\u52a0\uff0c\u8bf7\u8003\u8651\u4f7f\u7528 <code>LazyHStack<\/code> (\u82f1\u6587) \u548c <code>LazyVStack<\/code> (\u82f1\u6587) \u4ee3\u66ff <code>HStack<\/code> (\u82f1\u6587) \u548c <code>VStack<\/code> (\u82f1\u6587)\u3002\u60f0\u6027 Stack \u6309\u9700\u8f7d\u5165\u548c\u6e32\u67d3\u5b50\u89c6\u56fe\uff0c\u4ece\u800c\u5728\u8f7d\u5165\u5927\u91cf\u5b50\u89c6\u56fe\u65f6\u53ef\u63d0\u4f9b\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\u3002 <\/p>\n<\/p>\n<p> Stack \u89c6\u56fe\u548c\u60f0\u6027 Stack \u5177\u6709\u76f8\u4f3c\u7684\u529f\u80fd\uff0c\u53ef\u80fd\u7ed9\u4eba\u611f\u89c9\u5b83\u4eec\u80fd\u591f\u4e92\u6362\uff0c\u4f46\u662f\uff0c\u5b83\u4eec\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u6709\u5404\u81ea\u7684\u4f18\u52bf\u3002Stack \u89c6\u56fe\u4f1a\u4e00\u6b21\u6027\u8f7d\u5165\u6240\u6709\u5b50\u89c6\u56fe\uff0c\u5e03\u5c40\u6027\u80fd\u5feb\u4e14\u53ef\u9760\uff0c\u56e0\u4e3a\u7cfb\u7edf\u5728\u8f7d\u5165\u6bcf\u4e2a\u5b50\u89c6\u56fe\u65f6\u5c31\u77e5\u9053\u5b83\u4eec\u7684\u5927\u5c0f\u548c\u5f62\u72b6\u3002\u60f0\u6027 Stack \u4f1a\u4e3a\u4e86\u6027\u80fd\u800c\u635f\u5931\u4e00\u5b9a\u7a0b\u5ea6\u7684\u5e03\u5c40\u6b63\u786e\u6027\uff0c\u539f\u56e0\u662f\u7cfb\u7edf\u4ec5\u5728\u5b50\u89c6\u56fe\u53ef\u89c1\u65f6\u624d\u4f1a\u8ba1\u7b97\u5b83\u4eec\u7684\u51e0\u4f55\u7ed3\u6784\u3002 <\/p>\n<p> \u5728\u9009\u62e9\u8981\u4f7f\u7528\u7684 Stack \u89c6\u56fe\u7c7b\u578b\u65f6\uff0c\u59cb\u7ec8\u5148\u9009\u62e9\u6807\u51c6 Stack \u89c6\u56fe\uff0c\u5e76\u4e14\u4ec5\u5728\u5bf9\u4ee3\u7801\u8fdb\u884c\u6027\u80fd\u5206\u6790\u540e\u8bc1\u660e\u503c\u5f97\u63d0\u5347\u6027\u80fd\u65f6\u624d\u5207\u6362\u5230\u60f0\u6027 Stack\u3002 <\/p>\n<h3> \u8fdb\u884c\u6027\u80fd\u5206\u6790\u4ee5\u53d1\u73b0\u6027\u80fd\u95ee\u9898 <\/h3>\n<p> \u8003\u8651\u8981\u4f7f\u7528\u54ea\u79cd Stack \u65f6\uff0c\u8bf7\u4f7f\u7528 Instruments \u5de5\u5177\u5bf9\u4f60\u7684 App\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u4ee5\u786e\u5b9a\u7528\u6237\u754c\u9762\u4ee3\u7801\u4e2d\u5c06\u6709\u5927\u91cf\u89c6\u56fe\u8f7d\u5165 Stack \u7684\u533a\u57df\u3002 <\/p>\n<p> \u82e5\u8981\u5bf9 SwiftUI \u89c6\u56fe\u8f7d\u5165\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u8bf7\u6253\u5f00 Instruments \u5de5\u5177\uff0c\u65b9\u6cd5\u662f\u5728 Xcode\u201cProduct\u201d(\u4ea7\u54c1) \u83dc\u5355\u4e2d\u9009\u62e9\u201cProfile\u201d(\u6027\u80fd\u5206\u6790)\uff0c\u7136\u540e\u9009\u53d6 SwiftUI \u6027\u80fd\u5206\u6790\u6a21\u677f\u3002\u8be5\u6a21\u677f\u5c06\u8f7d\u5165\u56db\u4e2a instrument\uff1aView Body\u3001View Properties\u3001Core Animation Commits \u548c Time Profiler\u3002\u8fd9\u4e9b instrument \u7684\u7ec4\u5408\u4e3a\u4f60\u5bfb\u627e\u52a0\u5feb App \u6027\u80fd\u7684\u673a\u4f1a\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e0d\u9519\u7684\u8d77\u70b9\u3002 <\/p>\n<p> \u6ce8\u91ca <\/p>\n<p> \u5207\u52ff\u4f7f\u7528 iOS \u6a21\u62df\u5668\u5bf9\u4f60\u7684\u4ee3\u7801\u8fdb\u884c\u6027\u80fd\u5206\u6790\u3002\u8bf7\u59cb\u7ec8\u4f7f\u7528\u771f\u5b9e\u8bbe\u5907\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u3002 <\/p>\n<\/p>\n<p> \u5bf9\u4e0a\u8ff0\u4ee3\u7801\u8fdb\u884c\u6027\u80fd\u5206\u6790\u65f6\uff0cView Body instrument \u663e\u793a 1,000 \u4e2a <code>ProfileView<\/code> \u5b9e\u4f8b\u540c\u65f6\u4f5c\u4e3a <code>HStack<\/code> (\u82f1\u6587) \u8f7d\u5165\u5185\u5b58\u3002\u968f\u7740\u7cfb\u7edf\u8f7d\u5165\u6bcf\u4e2a\u4e2a\u4eba\u8d44\u6599\uff0c\u4f60\u8fd8\u4f1a\u770b\u5230\u76f8\u540c\u6570\u91cf\u7684 <code>Image<\/code> (\u82f1\u6587) \u89c6\u56fe\u8f7d\u5165\u3002 <\/p>\n<p> \u5728\u6b64\u60c5\u51b5\u4e0b\uff0c\u89e3\u51b3\u65b9\u6848\u662f\u5c06 <code>HStack<\/code> (\u82f1\u6587) \u66ff\u6362\u4e3a <code>LazyHStack<\/code> (\u82f1\u6587)\uff0c\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff1a <\/p>\n<pre><code><span><span><\/span> <span><span>ScrollView<\/span>(.horizontal) {<\/span><\/span><span><span><\/span> <span> <span>LazyHStack<\/span> {<\/span><\/span><span><span><\/span> <span> <span>ForEach<\/span>(profiles) { profile <span>in<\/span><\/span><\/span><span><span><\/span> <span> <span>ProfileView<\/span>(profile: profile)<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span>}<\/span><\/span><span><span><\/span> <span>.frame(maxWidth: <span>500<\/span>)<\/span><\/span><\/code>\n<\/pre>\n<p> \u8fd0\u884c\u53e6\u4e00\u6b21\u8ddf\u8e2a\u4f1a\u663e\u793a\u6700\u521d\u8f7d\u5165\u7684\u89c6\u56fe\u6570\u91cf\u6025\u5267\u4e0b\u964d\uff0c\u56e0\u4e3a\u53ea\u6709\u56db\u4e2a\u542f\u52a8\u7684 <code>ProfileView<\/code> \u5b9e\u4f8b\u53ef\u89c1\u3002\u4f60\u8fd8\u53ef\u4ee5\u5728\u201cTotal Duration\u201d(\u603b\u65f6\u95f4\u957f\u5ea6) \u5217\u4e2d\u770b\u5230\u76f8\u5e94\u7684\u4e0b\u964d\u3002 <\/p>\n<\/p>\n<p> \u6709\u5173\u4f7f\u7528 Instruments \u5de5\u5177\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u63d0\u5347 App \u7684\u6027\u80fd\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u89c8 \u4f60\u7684 App \u9700\u8981\u5728\u5bb9\u5668\u89c6\u56fe\u4e2d\u663e\u793a\u7684\u6570\u636e\u7ecf\u5e38\u4f1a\u8d85\u8fc7\u8bbe\u5907\u5c4f\u5e55\u4e0a\u5bb9\u8bb8\u7684\u7a7a\u95f4\u3002\u5bf9\u4e8e\u91cd\u590d\u89c6\u56fe\u6216\u89c6\u56fe\u7ec4\uff0c\u6c34\u5e73 St [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":190883,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189686],"tags":[],"class_list":["post-190882","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=190882"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190882\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/190883"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=190882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=190882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=190882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}