{"id":3941,"date":"2025-05-29T11:06:28","date_gmt":"2025-06-05T03:48:27","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3941"},"modified":"2025-06-05T11:06:28","modified_gmt":"2025-06-05T03:48:27","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e8%87%aa%e5%8b%95%e5%b8%83%e5%b1%80","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e8%87%aa%e5%8b%95%e5%b8%83%e5%b1%80\/","title":{"rendered":"\u638c\u63e1 Swift \u81ea\u52d5\u5e03\u5c40\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, NSLayoutConstraint, VisualFormatLanguage, \u81ea\u52d5\u5e03\u5c40, iOS\u958b\u767c, \u81ea\u52d5\u5e03\u5c40\u6700\u4f73\u5be6\u8e10\"><\/p>\n<h1>\u638c\u63e1 Swift \u81ea\u52d5\u5e03\u5c40\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u80fd\u5920\u8f15\u9b06\u958b\u767ciOS\u548cmacOS\u61c9\u7528\u7a0b\u5f0f\u3002\u5728Swift\u4e2d\uff0c\u81ea\u52d5\u5e03\u5c40\u662f\u4e00\u9805\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u6709\u6548\u6392\u7248\u61c9\u7528\u4ecb\u9762\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u81ea\u52d5\u5e03\u5c40\uff0c\u4e26\u6db5\u84cb\u4f7f\u7528NSLayoutConstraint\u985e\u548cVisualFormatLanguage\u7684\u6700\u65b0\u5be6\u4f5c\u65b9\u5f0f\u3002<\/p>\n<h2>\u4f7f\u7528 NSLayoutConstraint \u985e\u5be6\u73fe\u81ea\u52d5\u5e03\u5c40<\/h2>\n<p>NSLayoutConstraint\u985e\u662fSwift\u4e2d\u5be6\u73fe\u81ea\u52d5\u5e03\u5c40\u7684\u4e3b\u8981\u65b9\u6cd5\u4e4b\u4e00\u3002\u900f\u904e\u9019\u500b\u985e\u5225\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5b9a\u7fa9\u754c\u9762\u5143\u7d20\u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u4f8b\u5982\u9ad8\u5ea6\u3001\u5bec\u5ea6\u6216\u8ddd\u96e2\u7b49\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u8a2d\u7f6e\u4e00\u500bUIView\u7684\u9ad8\u5ea6\u70ba100\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet view = UIView()<br \/>\nview.translatesAutoresizingMaskIntoConstraints = false \/\/ \u78ba\u4fdd\u81ea\u52d5\u5e03\u5c40\u555f\u7528<br \/>\nlet heightConstraint = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100)<br \/>\nview.addConstraint(heightConstraint)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c`translatesAutoresizingMaskIntoConstraints`\u5c6c\u6027\u5fc5\u9808\u8a2d\u7f6e\u70ba`false`\u4ee5\u555f\u7528\u81ea\u52d5\u5e03\u5c40\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5275\u5efa\u4e00\u500bNSLayoutConstraint\u5c0d\u8c61\u4f86\u63a7\u5236UIView\u7684\u9ad8\u5ea6\u3002<\/p>\n<h2>\u4f7f\u7528 VisualFormatLanguage \u5be6\u73fe\u81ea\u52d5\u5e03\u5c40<\/h2>\n<p>VisualFormatLanguage\uff08VFL\uff09\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u4fbf\u7684\u65b9\u5f0f\u4f86\u63cf\u8ff0\u754c\u9762\u5143\u7d20\u4e4b\u9593\u7684\u95dc\u4fc2\u3002\u901a\u904e\u4f7f\u7528\u7c21\u55ae\u7684\u5b57\u7b26\u4e32\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u5b9a\u7fa9\u5e03\u5c40\u3002<\/p>\n<p>\u4ee5\u4e0b\u7bc4\u4f8b\u540c\u6a23\u8a2d\u7f6e\u4e00\u500bUIView\u7684\u9ad8\u5ea6\u70ba100\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet view = UIView()<br \/>\nview.translatesAutoresizingMaskIntoConstraints = false<br \/>\nlet views = [&#8220;view&#8221;: view]\nlet metrics = [&#8220;height&#8221;: 100]\nlet heightConstraints = NSLayoutConstraint.constraints(withVisualFormat: &#8220;V:[view(height)]&#8221;, options: [], metrics: metrics, views: views)<br \/>\nview.addConstraints(heightConstraints)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5229\u7528VFL\u5275\u5efa\u4e86\u4e00\u7d44NSLayoutConstraint\uff0c\u4e26\u5c07\u5176\u61c9\u7528\u65bcUIView\u5c0d\u8c61\uff0c\u5f9e\u800c\u5be6\u73fe\u81ea\u52d5\u5e03\u5c40\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528\u81ea\u52d5\u5e03\u5c40\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; **\u7d04\u675f\u885d\u7a81**\uff1a\u78ba\u4fdd\u6240\u6709\u7d04\u675f\u90fd\u662f\u4e00\u81f4\u7684\uff0c\u907f\u514d\u540c\u6642\u5c0d\u540c\u4e00\u5c6c\u6027\u8a2d\u7f6e\u591a\u500b\u7d04\u675f\u3002<br \/>\n&#8211; **\u672a\u8a2d\u7f6etranslatesAutoresizingMaskIntoConstraints**\uff1a\u5728\u4f7f\u7528\u81ea\u52d5\u5e03\u5c40\u6642\uff0c\u8a18\u5f97\u5c07\u9019\u500b\u5c6c\u6027\u8a2d\u70ba`false`\u3002<\/p>\n<p>\u6700\u4f73\u5be6\u8e10\u5305\u62ec\uff1a<br \/>\n&#8211; \u4f7f\u7528Stack Views\u7c21\u5316\u5e03\u5c40\u3002<br \/>\n&#8211; \u5118\u91cf\u5728Interface Builder\u4e2d\u8a2d\u8a08UI\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u7a0b\u5f0f\u78bc\u4e2d\u7684\u932f\u8aa4\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u81ea\u52d5\u5e03\u5c40\u4e0d\u50c5\u80fd\u7528\u65bcUIView\uff0c\u9084\u53ef\u4ee5\u61c9\u7528\u65bcUITableViewCell\u548cUICollectionViewCell\u7b49\u8907\u96dc\u7684\u8996\u5716\u5143\u7d20\u3002\u5229\u7528\u81ea\u52d5\u5e03\u5c40\uff0c\u958b\u767c\u8005\u80fd\u5920\u5275\u5efa\u97ff\u61c9\u5f0f\u8a2d\u8a08\uff0c\u9069\u61c9\u5404\u7a2e\u5c4f\u5e55\u5c3a\u5bf8\u548c\u65b9\u5411\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u81ea\u52d5\u5e03\u5c40\uff0c\u5305\u62ec\u4f7f\u7528NSLayoutConstraint\u985e\u548cVisualFormatLanguage\u7684\u6700\u65b0\u65b9\u6cd5\u3002\u900f\u904e\u9019\u4e9b\u6280\u8853\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u8a2d\u8a08\u61c9\u7528\u7a0b\u5f0f\u754c\u9762\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u70ba\u4ec0\u9ebc\u6211\u7684\u7d04\u675f\u4e0d\u751f\u6548\uff1f**<br \/>\nA1: \u8acb\u6aa2\u67e5\u662f\u5426\u5df2\u5c07`translatesAutoresizingMaskIntoConstraints`\u5c6c\u6027\u8a2d\u70ba`false`\uff0c\u4e26\u78ba\u4fdd\u6240\u6709\u7d04\u675f\u4e0d\u6703\u885d\u7a81\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528Stack Views\uff1f**<br \/>\nA2: Stack Views\u53ef\u4ee5\u81ea\u52d5\u7ba1\u7406\u5176\u5b50\u8996\u5716\u7684\u5e03\u5c40\uff0c\u60a8\u53ef\u4ee5\u900f\u904e\u7c21\u55ae\u5730\u5c07\u8996\u5716\u6dfb\u52a0\u5230Stack View\u4f86\u5be6\u73fe\u81ea\u52d5\u5e03\u5c40\uff0c\u7121\u9700\u624b\u52d5\u8a2d\u7f6e\u7d04\u675f\u3002<\/p>\n<p>**Q3: VisualFormatLanguage\u6709\u4ec0\u9ebc\u512a\u52e2\uff1f**<br \/>\nA3: VFL\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u6f54\u7684\u65b9\u5f0f\u4f86\u63cf\u8ff0\u5e03\u5c40\uff0c\u53ef\u4ee5\u5feb\u901f\u7de8\u5beb\u591a\u500b\u7d04\u675f\uff0c\u7279\u5225\u9069\u5408\u65bc\u9700\u8981\u5927\u91cf\u7d04\u675f\u7684\u60c5\u6cc1\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u81ea\u52d5\u5e03\u5c40\uff0c\u5305\u62ec\u4f7f\u7528NSLayoutConstraint\u7c7b\u5b9e\u73b0\u81ea\u52d5\u5e03\u5c40\u3001\u4f7f\u7528VisualFormatLanguage\u5b9e\u73b0\u81ea\u52d5\u5e03\u5c40\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","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-3941","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-11z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3941","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=3941"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3941\/revisions"}],"predecessor-version":[{"id":3942,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3941\/revisions\/3942"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}