{"id":7034,"date":"2025-05-31T13:43:33","date_gmt":"2025-06-05T06:11:32","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7034"},"modified":"2025-06-05T13:43:33","modified_gmt":"2025-06-05T06:11:32","slug":"https-badgameshow-com-steven-193","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-193\/","title":{"rendered":"Swift \u81ea\u52d5\u5e03\u5c40\uff1a\u4f7f\u7528 NSLayoutConstraint \u5b8c\u6210\u756b\u9762\u914d\u7f6e\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, NSLayoutConstraint, \u81ea\u52d5\u5e03\u5c40, iOS, macOS\"><\/p>\n<h1>Swift \u81ea\u52d5\u5e03\u5c40\uff1a\u4f7f\u7528 NSLayoutConstraint \u5b8c\u6210\u756b\u9762\u914d\u7f6e\u7684\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u4e14\u73fe\u4ee3\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc iOS \u548c macOS \u7684\u61c9\u7528\u958b\u767c\u3002\u5176\u4e2d\uff0c\u81ea\u52d5\u5e03\u5c40\uff08Auto Layout\uff09\u662f\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u9748\u6d3b\u5730\u5b89\u6392\u4ecb\u9762\u5143\u4ef6\uff0c\u7121\u9700\u624b\u52d5\u8a08\u7b97\u6bcf\u500b\u5143\u4ef6\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<p>\u5728 Swift \u4e2d\uff0c\u81ea\u52d5\u5e03\u5c40\u662f\u900f\u904e **NSLayoutConstraint** \u985e\u5225\u4f86\u5be6\u73fe\u7684\u3002\u9019\u500b\u985e\u5225\u7528\u65bc\u63cf\u8ff0\u4ecb\u9762\u5143\u4ef6\u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u5982\u4f4d\u7f6e\u3001\u5927\u5c0f\u548c\u9593\u8ddd\u3002\u6b63\u78ba\u4f7f\u7528 NSLayoutConstraint \u53ef\u4ee5\u4f7f\u5f97\u4ecb\u9762\u5728\u4e0d\u540c\u8a2d\u5099\u548c\u986f\u793a\u5c4f\u4e0a\u81ea\u52d5\u9069\u61c9\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>NSLayoutConstraint \u7684\u4f7f\u7528\u6b65\u9a5f<\/h2>\n<p>\u4f7f\u7528 NSLayoutConstraint \u4f86\u9032\u884c\u81ea\u52d5\u5e03\u5c40\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5efa\u7acb\u4e00\u500b NSLayoutConstraint \u7269\u4ef6\uff0c\u4e26\u6307\u5b9a\u5169\u500b\u4ecb\u9762\u5143\u4ef6\u4e4b\u9593\u7684\u95dc\u4fc2\u3002<\/li>\n<li>\u5c07 NSLayoutConstraint \u7269\u4ef6\u6dfb\u52a0\u5230\u4ecb\u9762\u5143\u4ef6\u7684\u7236\u8996\u5716\u4e2d\u3002<\/li>\n<li>\u78ba\u4fdd\u4ecb\u9762\u5143\u4ef6\u7684\u7236\u8996\u5716\u7684 <code>translatesAutoresizingMaskIntoConstraints<\/code> \u5c6c\u6027\u8a2d\u7f6e\u70ba <code>false<\/code>\u3002<\/li>\n<\/ol>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b\uff1a\u5c07 UIView \u5143\u4ef6\u5b89\u6392\u5728\u756b\u9762\u7684\u53f3\u4e0a\u89d2<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 NSLayoutConstraint \u5c07\u4e00\u500b UIView \u5143\u4ef6\u5b89\u6392\u5728\u756b\u9762\u7684\u53f3\u4e0a\u89d2\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5efa\u7acb\u4e00\u500b UIView \u5143\u4ef6\nlet view = UIView()\n\n\/\/ \u8a2d\u5b9a UIView \u7684\u80cc\u666f\u984f\u8272\nview.backgroundColor = .blue\n\n\/\/ \u78ba\u4fdd UIView \u7684\u7236\u8996\u5716\u4e0d\u6703\u81ea\u52d5\u8f49\u63db\u81ea\u52d5\u8abf\u6574\u7684\u5c6c\u6027\nview.superview?.translatesAutoresizingMaskIntoConstraints = false\n\n\/\/ \u5efa\u7acb\u4e00\u500b NSLayoutConstraint \u7269\u4ef6\uff0c\u6307\u5b9a UIView \u5143\u4ef6\u7684\u53f3\u908a\u8207\u7236\u8996\u5716\u7684\u53f3\u908a\u7684\u8ddd\u96e2\u70ba 10\nlet rightConstraint = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: view.superview, attribute: .right, multiplier: 1.0, constant: -10)\n\n\/\/ \u5c07 NSLayoutConstraint \u7269\u4ef6\u52a0\u5165\u5230 UIView \u5143\u4ef6\u7684\u7236\u8996\u5716\u4e2d\nview.superview?.addConstraint(rightConstraint)\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 NSLayoutConstraint \u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<p>&#8211; **\u7d04\u675f\u672a\u751f\u6548**\uff1a\u78ba\u8a8d\u7236\u8996\u5716\u7684 <code>translatesAutoresizingMaskIntoConstraints<\/code> \u5c6c\u6027\u8a2d\u7f6e\u70ba <code>false<\/code>\u3002<br \/>\n&#8211; **\u7d04\u675f\u885d\u7a81**\uff1a\u6aa2\u67e5\u662f\u5426\u6709\u5176\u4ed6\u7d04\u675f\u5f71\u97ff\u5230\u540c\u4e00\u500b\u5143\u4ef6\uff0c\u5c0e\u81f4\u885d\u7a81\u767c\u751f\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u4f7f\u7528\u65b9\u5f0f\uff0cNSLayoutConstraint \u9084\u53ef\u4ee5\u7d50\u5408\u52d5\u756b\u6548\u679c\u4f86\u5f37\u5316\u7528\u6236\u4ecb\u9762\u7684\u4e92\u52d5\u6027\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u52d5\u756b\u4f86\u6539\u8b8a\u7d04\u675f\u5c6c\u6027\uff0c\u5be6\u73fe\u6d41\u66a2\u7684\u4ecb\u9762\u8b8a\u5316\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e\u4f7f\u7528 NSLayoutConstraint\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u57f7\u884c\u81ea\u52d5\u5e03\u5c40\uff0c\u8b93\u61c9\u7528\u7a0b\u5f0f\u5728\u5404\u7a2e\u5c4f\u5e55\u5c3a\u5bf8\u4e0b\u8868\u73fe\u51fa\u8272\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u9084\u80fd\u63d0\u5347\u6700\u7d42\u7528\u6236\u7684\u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u4ec0\u4e48\u662f NSLayoutConstraint\uff1f<\/h3>\n<p>NSLayoutConstraint \u662f\u7528\u65bc\u63cf\u8ff0\u4ecb\u9762\u5143\u4ef6\u4e4b\u9593\u95dc\u4fc2\u7684\u985e\u5225\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5be6\u73fe\u81ea\u52d5\u5e03\u5c40\u3002<\/p>\n<h3>2. \u5982\u4f55\u78ba\u4fdd\u7d04\u675f\u751f\u6548\uff1f<\/h3>\n<p>\u5fc5\u9808\u5c07\u7236\u8996\u5716\u7684 <code>translatesAutoresizingMaskIntoConstraints<\/code> \u5c6c\u6027\u8a2d\u7f6e\u70ba <code>false<\/code>\uff0c\u4ee5\u78ba\u4fdd\u81ea\u52d5\u5e03\u5c40\u7684\u7d04\u675f\u751f\u6548\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u4f7f\u7528 NSLayoutConstraint \u9032\u884c\u52d5\u756b\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u52d5\u756b\u4f86\u6539\u8b8a NSLayoutConstraint \u7684\u5c6c\u6027\uff0c\u5f9e\u800c\u5be6\u73fe\u6d41\u66a2\u7684\u4ecb\u9762\u8b8a\u5316\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u81ea\u52d5\u5e03\u5c40-\u4f7f\u7528NSLayoutConstraint-\ud83d\udcaa_1675401070.394871.webp\" alt=\"Swift \u81ea\u52d5\u5e03\u5c40 \u4f7f\u7528NSLayoutConstraint \ud83d\udcaa\" title=\"Swift \u81ea\u52d5\u5e03\u5c40\u3001NSLayoutConstraint\u3001iOS\u3001macOS\u3001\u4ecb\u9762\" width=\"1200\" height=\"628\"\/><\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u5b78\u7fd2Swift\u81ea\u52d5\u5e03\u5c40\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u8f15\u9b06\u5730\u5728iOS\u548cmacOS\u4e0a\u5efa\u7acb\u61c9\u7528\u7a0b\u5f0f\u7684\u4ecb\u9762\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528NSLayoutConstraint\u4f86\u5efa\u7acb\u81ea\u52d5\u5e03\u5c40\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u7bc4\u4f8b\u4f86\u8b93\u4f60\u66f4\u5feb\u5730\u4e0a\u624b\u3002<\/p>\n","protected":false},"author":1,"featured_media":7033,"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-7034","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\/Swift-\u81ea\u52d5\u5e03\u5c40-\u4f7f\u7528NSLayoutConstraint-\ud83d\udcaa_1675401070.394871.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Ps","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7034","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=7034"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7034\/revisions"}],"predecessor-version":[{"id":13276,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7034\/revisions\/13276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7033"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}