{"id":190886,"date":"2023-01-20T11:55:51","date_gmt":"2023-01-20T03:55:51","guid":{"rendered":"https:\/\/www.idc.net\/help\/190886\/"},"modified":"2023-01-20T11:55:51","modified_gmt":"2023-01-20T03:55:51","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91ios%e6%95%99%e7%a8%8b%ef%bc%9a%e4%bd%bf%e7%94%a8-stack-%e8%a7%86%e5%9b%be%e6%9e%84%e5%bb%ba%e5%b8%83%e5%b1%80","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/190886\/","title":{"rendered":"\u540e\u6d6a\u4e91IOS\u6559\u7a0b\uff1a\u4f7f\u7528 Stack \u89c6\u56fe\u6784\u5efa\u5e03\u5c40"},"content":{"rendered":"<h2> \u6982\u89c8 <\/h2>\n<p> \u5355\u72ec\u7684 <code>HStack<\/code> (\u82f1\u6587)\u3001<code>VStack<\/code> (\u82f1\u6587) \u548c <code>ZStack<\/code> (\u82f1\u6587) \u5747\u5c5e\u4e8e\u7b80\u5355\u89c6\u56fe\u3002<code>HStack<\/code> (\u82f1\u6587) \u53ef\u4ee5\u5c06\u89c6\u56fe\u7f6e\u4e8e\u6c34\u5e73\u7ebf\u4e0a\uff0c<code>VStack<\/code> (\u82f1\u6587) \u53ef\u4ee5\u5c06\u89c6\u56fe\u7f6e\u4e8e\u5782\u76f4\u7ebf\u4e0a\uff0c\u800c <code>ZStack<\/code> (\u82f1\u6587) \u5219\u53ef\u4ee5\u5c06\u89c6\u56fe\u76f8\u4e92\u53e0\u653e\u3002 <\/p>\n<\/p>\n<p> \u5f53\u4f60\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\u521d\u59cb\u5316\u5b83\u4eec\u65f6\uff0cstack \u89c6\u56fe\u4f1a\u5c06\u5176\u5185\u5bb9\u5c45\u4e2d\u5bf9\u9f50\uff0c\u5e76\u5728\u6bcf\u4e2a\u6240\u542b\u89c6\u56fe\u4e4b\u95f4\u63d2\u5165\u5c11\u91cf\u95f4\u9694\u3002\u4f46\u662f\uff0c\u5f53\u4f60\u4f7f\u7528\u89c6\u56fe\u4fee\u9970\u7b26 <code>Spacer<\/code> (\u82f1\u6587) \u548c <code>Divider<\/code> (\u82f1\u6587) \u89c6\u56fe\u5408\u5e76\u548c\u81ea\u5b9a stack \u65f6\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u9ad8\u5ea6\u7075\u6d3b\u7684\u590d\u6742\u5e03\u5c40\u3002 <\/p>\n<h3> \u89c4\u5212\u4f60\u7684\u5e03\u5c40\u5c42\u6b21\u7ed3\u6784 <\/h3>\n<p> \u5f53\u4f60\u5f00\u59cb\u5c06\u8bbe\u8ba1\u8f6c\u6362\u4e3a\u4ee3\u7801\u65f6\uff0c\u5728\u5e03\u5c40\u65b9\u9762\uff0c\u8bf7\u8003\u8651\u53ef\u4ee5\u5982\u4f55\u4f7f\u7528\u5404\u79cd\u7c7b\u578b\u7684 stack \u89c6\u56fe\u521b\u5efa\u5e03\u5c40\u3002\u5c06\u590d\u6742\u8bbe\u8ba1\u7ec6\u5206\u4e3a\u4f60\u53ef\u4ee5\u901a\u8fc7 stack \u89c6\u56fe\u6784\u5efa\u7684\u66f4\u5c0f\u3001\u66f4\u7b80\u5355\u7684\u90e8\u5206\u3002 <\/p>\n<p> \u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e09\u79cd stack \u89c6\u56fe\u6784\u5efa\u4ee5\u4e0b\u4e2a\u4eba\u8d44\u6599\u89c6\u56fe\uff1a <\/p>\n<\/p>\n<p> <code>ZStack<\/code> (\u82f1\u6587) \u5305\u542b\u4e00\u4e2a\u663e\u793a\u4e2a\u4eba\u8d44\u6599\u56fe\u7247\u7684 <code>Image<\/code> (\u82f1\u6587) \u89c6\u56fe\uff0c\u540e\u8005\u9876\u90e8\u53e0\u52a0\u4e86\u4e00\u4e2a\u534a\u900f\u660e <code>HStack<\/code> (\u82f1\u6587)\u3002<code>HStack<\/code> (\u82f1\u6587) \u5305\u542b\u4e00\u4e2a <code>VStack<\/code> (\u82f1\u6587)\uff0c\u540e\u8005\u5185\u90e8\u6709\u4e00\u5bf9 <code>Text<\/code> (\u82f1\u6587) \u89c6\u56fe\uff0c<code>Spacer<\/code> (\u82f1\u6587) \u89c6\u56fe\u5c06 <code>VStack<\/code> (\u82f1\u6587) \u63a8\u5411\u524d\u7f18\u4fa7\u3002 <\/p>\n<p> \u82e5\u8981\u521b\u5efa\u8fd9\u4e2a stack \u89c6\u56fe\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a <\/p>\n<pre><code><span><span><\/span> <span><span><span>struct<\/span> <span>ProfileView<\/span>: <span>View<\/span> <\/span>{<\/span><\/span><span><span><\/span> <span> <span>var<\/span> body: <span>some<\/span> <span>View<\/span> {<\/span><\/span><span><span><\/span> <span> <span>ZStack<\/span>(alignment: .bottom) {<\/span><\/span><span><span><\/span> <span> <span>Image<\/span>(<span>\"ProfilePicture\"<\/span>)<\/span><\/span><span><span><\/span> <span> .resizable()<\/span><\/span><span><span><\/span> <span> .aspectRatio(contentMode: .fit)<\/span><\/span><span><span><\/span> <span> <span>HStack<\/span> {<\/span><\/span><span><span><\/span> <span> <span>VStack<\/span>(alignment: .leading) {<\/span><\/span><span><span><\/span> <span> <span>Text<\/span>(<span>\"Rachael Chiseck\"<\/span>)<\/span><\/span><span><span><\/span> <span> .font(.headline)<\/span><\/span><span><span><\/span> <span> <span>Text<\/span>(<span>\"Chief Executive Officer\"<\/span>)<\/span><\/span><span><span><\/span> <span> .font(.subheadline)<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span> <span>Spacer<\/span>()<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span> .padding()<\/span><\/span><span><span><\/span> <span> .foregroundColor(.primary)<\/span><\/span><span><span><\/span> <span> .background(<span>Color<\/span>.primary<\/span><\/span><span><span><\/span> <span> .colorInvert()<\/span><\/span><span><span><\/span> <span> .opacity(<span>0.75<\/span>))<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span>}<\/span><\/span><\/code>\n<\/pre>\n<h3> \u901a\u8fc7 Alignment \u548c Spacer \u89c6\u56fe\u786e\u5b9a\u89c6\u56fe\u4f4d\u7f6e <\/h3>\n<p> \u901a\u8fc7\u7ec4\u5408\u4f7f\u7528 <code>alignment<\/code> \u5c5e\u6027\u3001<code>Spacer<\/code> (\u82f1\u6587) \u548c <code>Divider<\/code> (\u82f1\u6587) \u89c6\u56fe\uff0c\u5bf9\u9f50 stack \u89c6\u56fe\u4e2d\u5305\u542b\u7684\u4efb\u610f\u89c6\u56fe\u3002 <\/p>\n<p> \u5728\u524d\u9762\u7684\u793a\u4f8b\u5e03\u5c40\u4e2d\uff0c\u5305\u542b\u4e24\u4e2a <code>Text<\/code> (\u82f1\u6587) \u89c6\u56fe\u7684 <code>VStack<\/code> (\u82f1\u6587) \u4f7f\u7528 <code>leading<\/code> (\u82f1\u6587) \u5bf9\u9f50\uff1a <\/p>\n<\/p>\n<p> <code>alignment<\/code> \u5c5e\u6027\u4e0d\u4f1a\u8c03\u6574 <code>VStack<\/code> (\u82f1\u6587) \u76f8\u5bf9\u4e8e\u5176\u7236\u5bb9\u5668\u7684\u5e03\u5c40\uff0c\u800c\u662f\u8c03\u6574 <code>VStack<\/code> (\u82f1\u6587) \u5185\u90e8\u89c6\u56fe\u7684\u5e03\u5c40\u3002 <\/p>\n<p> <code>VStack<\/code> (\u82f1\u6587) \u7684 <code>alignment<\/code> \u5c5e\u6027\u4ec5\u4f1a\u5e94\u7528\u4e8e\u4f7f\u7528 <code>HorizontalAlignment<\/code> (\u82f1\u6587) \u5bf9\u6240\u542b\u63a7\u4ef6\u7684\u6c34\u5e73\u5bf9\u9f50\u3002\u540c\u6837\uff0c<code>HStack<\/code> (\u82f1\u6587) \u7684 <code>alignment<\/code> \u5c5e\u6027\u4ec5\u63a7\u5236\u4f7f\u7528 <code>VerticalAlignment<\/code> (\u82f1\u6587) \u7684\u5782\u76f4\u5bf9\u9f50\u3002\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>Alignment<\/code> (\u82f1\u6587) \u6cbf\u4e24\u4e2a\u8f74\u5bf9\u9f50 <code>ZStack<\/code> (\u82f1\u6587) \u4e2d\u7684\u89c6\u56fe\u3002 <\/p>\n<p> <code>Spacer<\/code> (\u82f1\u6587) \u89c6\u56fe\u7528\u4e8e\u6cbf <code>HStack<\/code> (\u82f1\u6587) \u6216 <code>VStack<\/code> (\u82f1\u6587) \u7684\u4e3b\u8f74\u5bf9\u9f50\u89c6\u56fe\u3002Spacer \u4f1a\u8fdb\u884c\u6269\u5c55\u4ee5\u586b\u5145\u6240\u6709\u53ef\u7528\u7a7a\u95f4\uff0c\u5e76\u5c06\u5185\u5bb9\u4ece\u5176\u4ed6\u89c6\u56fe\u6216 stack \u7684\u8fb9\u7f18\u63a8\u5f00\u3002 <\/p>\n<\/p>\n<p> <code>Divider<\/code> (\u82f1\u6587) \u89c6\u56fe\u8fd8\u4f1a\u5728 stack \u7684\u5b50\u89c6\u56fe\u4e4b\u95f4\u6dfb\u52a0\u7a7a\u95f4\uff0c\u4f46\u4ec5\u4f1a\u63d2\u5165\u8db3\u591f\u6cbf stack \u7684\u526f\u8f74\u7ed8\u5236\u4e00\u6761\u76f4\u7ebf\u7684\u7a7a\u95f4\u3002\u5b83\u4eec\u4e0d\u4f1a\u8fdb\u884c\u6269\u5c55\u6765\u586b\u5145\u53ef\u7528\u7684\u7a7a\u95f4\u3002 <\/p>\n<h3> \u521b\u5efa\u81ea\u9002\u5e94\u5e03\u5c40\u800c\u4e0d\u662f\u663e\u5f0f\u5e03\u5c40 <\/h3>\n<p> \u5c3d\u53ef\u80fd\u5b9a\u4e49\u7ed3\u6784\u548c\u5c42\u6b21\u7ed3\u6784\uff0c\u800c\u4e0d\u662f\u663e\u5f0f\u786e\u5b9a\u89c6\u56fe\u6846\u67b6\u7684\u4f4d\u7f6e\u3002\u4e0d\u8981\u4e3a\u89c6\u56fe\u4f7f\u7528\u663e\u5f0f\u9ad8\u5ea6\u548c\u5bbd\u5ea6\uff0c\u800c\u5e94\u8ba9\u5b83\u4eec\u8fdb\u884c\u6269\u5c55\u4ee5\u586b\u5145\u53ef\u7528\u7a7a\u95f4\u3002\u4f60\u6784\u5efa\u7684\u81ea\u9002\u5e94\u5e03\u5c40\u66f4\u5bb9\u6613\u9002\u5e94\u4e0d\u540c\u7684\u8bbe\u5907\u5927\u5c0f\u548c\u5e73\u53f0\u3002 <\/p>\n<p> \u901a\u8fc7\u4ee5\u663e\u5f0f\u65b9\u5f0f\u64cd\u63a7 <code>Text<\/code> (\u82f1\u6587) \u89c6\u56fe\u6846\u67b6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u800c\u4e0d\u662f\u4e09\u4e2a stack \u89c6\u56fe\u6765\u521b\u5efa\u8fd9\u7bc7\u6587\u7ae0\u7684\u793a\u4f8b\u5e03\u5c40\u3002\u5c3d\u7ba1\u8f93\u51fa\u770b\u8d77\u6765\u53ef\u80fd\u76f8\u540c\uff0c\u4f46\u7528\u6765\u5b9e\u65bd\u7684\u4ee3\u7801\u66f4\u52a0\u8106\u5f31\uff0c\u5e76\u4e14\u8fd8\u53ef\u80fd\u65e0\u6cd5\u5728\u4e0d\u540c\u5927\u5c0f\u7c7b\u522b\u7684\u8bbe\u5907\u4e4b\u95f4\u8fdb\u884c\u7f29\u653e\u3002 <\/p>\n<p> \u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528 <code>frame(width:height:alignment:)<\/code> (\u82f1\u6587) \u6216 <code>position(x:y:)<\/code> (\u82f1\u6587) \u7b49\u89c6\u56fe\u4fee\u9970\u7b26\u6765\u5bf9\u4f7f\u7528\u663e\u5f0f\u8c03\u6574\u7684\u5e03\u5c40\u8fdb\u884c\u8c03\u6574\uff0c\u4f46\u4ec5\u5f53\u4f60\u65e0\u6cd5\u4ee5\u7075\u6d3b\u7684\u81ea\u9002\u5e94\u65b9\u5f0f\u83b7\u5f97\u6240\u9700\u5e03\u5c40\u624d\u5e94\u8003\u8651\u6b64\u65b9\u6cd5\u3002\u6709\u5173\u5bf9\u89c6\u56fe\u5e03\u5c40\u8fdb\u884c\u7cbe\u7ec6\u8c03\u6574\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u201c\u5bf9\u89c6\u56fe\u7684\u4f4d\u7f6e\u8fdb\u884c\u7cbe\u7ec6\u8c03\u6574 (\u82f1\u6587)\u201d\u3002 <\/p>\n<h3> \u4ee5\u66ff\u4ee3\u65b9\u5f0f\u6dfb\u52a0\u6df1\u5ea6 <\/h3>\n<p> \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528 <code>overlay(_:alignment:)<\/code> (\u82f1\u6587) \u548c <code>background(_:alignment:)<\/code> (\u82f1\u6587) \u89c6\u56fe\u4fee\u9970\u7b26\u800c\u4e0d\u662f <code>ZStack<\/code> (\u82f1\u6587) \u6765\u4e3a\u4f60\u7684\u5e03\u5c40\u6dfb\u52a0\u6df1\u5ea6\u3002Background \u89c6\u56fe\u4fee\u9970\u7b26\u5c06\u53e6\u4e00\u4e2a\u89c6\u56fe\u7f6e\u4e8e\u4f60\u6b63\u5728\u4fee\u9970\u7684\u89c6\u56fe\u4e4b\u540e\uff0coverlay \u5219\u662f\u5728\u5176\u9876\u90e8\u653e\u7f6e\u4e00\u4e2a\u89c6\u56fe\u3002 <\/p>\n<p> \u6839\u636e\u4f60\u60f3\u8981\u5982\u4f55\u786e\u5b9a\u6700\u7ec8\u5e03\u5c40\u7684\u5927\u5c0f\uff0c\u5728\u57fa\u4e8e stack \u7684\u65b9\u6cd5\u548c\u89c6\u56fe\u4fee\u9970\u7b26\u65b9\u6cd5\u4e4b\u95f4\u8fdb\u884c\u9009\u62e9\u3002\u5982\u679c\u4f60\u7684\u5e03\u5c40\u6709\u4e00\u4e2a\u5b9a\u4e49\u5e03\u5c40\u5927\u5c0f\u7684\u4e3b\u5bfc\u89c6\u56fe\uff0c\u8bf7\u5728\u8be5\u89c6\u56fe\u4e0a\u4f7f\u7528 <code>overlay(_:alignment:)<\/code> (\u82f1\u6587) \u6216 <code>background(_:alignment:)<\/code> (\u82f1\u6587) \u89c6\u56fe\u4fee\u9970\u7b26\u3002\u5982\u679c\u4f60\u60f3\u901a\u8fc7\u6c47\u603b\u6240\u6709\u6240\u542b\u89c6\u56fe\u5f97\u51fa\u6700\u7ec8\u89c6\u56fe\u5927\u5c0f\uff0c\u8bf7\u4f7f\u7528 <code>ZStack<\/code> (\u82f1\u6587)\u3002 <\/p>\n<p> \u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4f1a\u5728 <code>Image<\/code> (\u82f1\u6587) \u89c6\u56fe\u7684\u9876\u90e8\u53e0\u52a0\u4e00\u4e2a <code>ProfileDetail<\/code> \u89c6\u56fe\uff1a <\/p>\n<pre><code><span><span><\/span> <span><span><span>struct<\/span> <span>ProfileViewWithOverlay<\/span>: <span>View<\/span> <\/span>{<\/span><\/span><span><span><\/span> <span> <span>var<\/span> body: <span>some<\/span> <span>View<\/span> {<\/span><\/span><span><span><\/span> <span> <span>VStack<\/span> {<\/span><\/span><span><span><\/span> <span> <span>Image<\/span>(<span>\"ProfilePicture\"<\/span>)<\/span><\/span><span><span><\/span> <span> .resizable()<\/span><\/span><span><span><\/span> <span> .aspectRatio(contentMode: .fit)<\/span><\/span><span><span><\/span> <span> .overlay(<span>ProfileDetail<\/span>(), alignment: .bottom)<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span>}<\/span><\/span><span><span><\/span> <span> <\/span><\/span><span><span><\/span> <span><span><span>struct<\/span> <span>ProfileDetail<\/span>: <span>View<\/span> <\/span>{<\/span><\/span><span><span><\/span> <span> <span>var<\/span> body: <span>some<\/span> <span>View<\/span> {<\/span><\/span><span><span><\/span> <span> <span>HStack<\/span> {<\/span><\/span><span><span><\/span> <span> <span>VStack<\/span>(alignment: .leading) {<\/span><\/span><span><span><\/span> <span> <span>Text<\/span>(<span>\"Rachael Chiseck\"<\/span>)<\/span><\/span><span><span><\/span> <span> .font(.headline)<\/span><\/span><span><span><\/span> <span> <span>Text<\/span>(<span>\"Chief Executive Officer\"<\/span>)<\/span><\/span><span><span><\/span> <span> .font(.subheadline)<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span> <span>Spacer<\/span>()<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span> .padding()<\/span><\/span><span><span><\/span> <span> .foregroundColor(.primary)<\/span><\/span><span><span><\/span> <span> .background(<span>Color<\/span>.primary<\/span><\/span><span><span><\/span> <span> .colorInvert()<\/span><\/span><span><span><\/span> <span> .opacity(<span>0.75<\/span>))<\/span><\/span><span><span><\/span> <span> }<\/span><\/span><span><span><\/span> <span>}<\/span><\/span><\/code>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u89c8 \u5355\u72ec\u7684 HStack (\u82f1\u6587)\u3001VStack (\u82f1\u6587) \u548c ZStack (\u82f1\u6587) \u5747\u5c5e\u4e8e\u7b80\u5355\u89c6\u56fe\u3002HS [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":190887,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189686],"tags":[],"class_list":["post-190886","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\/190886","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=190886"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/190887"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=190886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=190886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=190886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}