{"id":3979,"date":"2025-05-31T11:46:36","date_gmt":"2025-06-05T03:52:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3979"},"modified":"2025-06-05T11:46:36","modified_gmt":"2025-06-05T03:52:36","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%e5%88%86%e6%ae%b5%e6%8e%a7%e4%bb%b6","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%e5%88%86%e6%ae%b5%e6%8e%a7%e4%bb%b6\/","title":{"rendered":"2025 \u5e74\u6700\u65b0 Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u4f7f\u7528 UISegmentedControl \u5275\u5efa\u5206\u6bb5\u63a7\u4ef6"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, UISegmentedControl, \u5206\u6bb5\u63a7\u4ef6, \u4e8b\u4ef6\u8655\u7406, iOS \u958b\u767c\"><\/p>\n<h1>2025 \u5e74\u6700\u65b0 Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u4f7f\u7528 UISegmentedControl \u5275\u5efa\u5206\u6bb5\u63a7\u4ef6<\/h1>\n<p>\u5728 Swift \u4e2d\uff0c\u5206\u6bb5\u63a7\u4ef6\uff08UISegmentedControl\uff09\u662f\u4e00\u7a2e\u5e38\u7528\u7684\u7528\u6236\u754c\u9762\u5143\u7d20\uff0c\u80fd\u5e6b\u52a9\u7528\u6236\u5728\u591a\u500b\u9078\u9805\u4e4b\u9593\u5feb\u901f\u5207\u63db\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 UISegmentedControl \u985e\u4f86\u5be6\u73fe\u5206\u6bb5\u63a7\u4ef6\uff0c\u5305\u62ec\u5982\u4f55\u8a2d\u7f6e\u5206\u6bb5\u63a7\u4ef6\u7684\u6a19\u984c\u3001\u5982\u4f55\u7372\u53d6\u7576\u524d\u9078\u4e2d\u7d22\u5f15\u3001\u5982\u4f55\u70ba\u5206\u6bb5\u63a7\u4ef6\u6dfb\u52a0\u4e8b\u4ef6\u8655\u7406\u65b9\u6cd5\u7b49\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u548c\u932f\u8aa4\u6392\u9664\u5efa\u8b70\u3002<\/p>\n<h2>\u4f7f\u7528 UISegmentedControl \u985e\u5be6\u73fe\u5206\u6bb5\u63a7\u4ef6<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b UISegmentedControl \u5be6\u4f8b\uff0c\u4e26\u5c07\u5176\u6dfb\u52a0\u5230\u8996\u5716\u4e2d\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet segmentedControl = UISegmentedControl()<br \/>\nsegmentedControl.translatesAutoresizingMaskIntoConstraints = false \/\/ \u4f7f\u5176\u81ea\u52d5\u5e03\u5c40<br \/>\nview.addSubview(segmentedControl)<\/p>\n<p>\/\/ \u8a2d\u7f6e\u7d04\u675f\u4ee5\u5b9a\u4f4d\u5206\u6bb5\u63a7\u4ef6<br \/>\nNSLayoutConstraint.activate([<br \/>\n    segmentedControl.centerXAnchor.constraint(equalTo: view.centerXAnchor),<br \/>\n    segmentedControl.centerYAnchor.constraint(equalTo: view.centerYAnchor)<br \/>\n])<br \/>\n&#8220;`<\/p>\n<h2>\u8a2d\u7f6e\u5206\u6bb5\u63a7\u4ef6\u7684\u6a19\u984c<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `insertSegment(withTitle:at:animated:)` \u65b9\u6cd5\u4f86\u70ba\u5206\u6bb5\u63a7\u4ef6\u6dfb\u52a0\u6a19\u984c\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nsegmentedControl.insertSegment(withTitle: &#8220;\u9078\u9805 1&#8221;, at: 0, animated: true)<br \/>\nsegmentedControl.insertSegment(withTitle: &#8220;\u9078\u9805 2&#8221;, at: 1, animated: true)<br \/>\nsegmentedControl.insertSegment(withTitle: &#8220;\u9078\u9805 3&#8221;, at: 2, animated: true)<br \/>\n&#8220;`<\/p>\n<p>\u78ba\u4fdd\u6a19\u984c\u7684\u8a9e\u8a00\u7b26\u5408\u4f60\u7684\u61c9\u7528\u9700\u6c42\uff0c\u9019\u6709\u52a9\u65bc\u589e\u5f37\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>\u7372\u53d6\u5206\u6bb5\u63a7\u4ef6\u7684\u7576\u524d\u9078\u4e2d\u7d22\u5f15<\/h2>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `selectedSegmentIndex` \u5c6c\u6027\u4f86\u7372\u53d6\u5206\u6bb5\u63a7\u4ef6\u7684\u7576\u524d\u9078\u4e2d\u7d22\u5f15\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet selectedIndex = segmentedControl.selectedSegmentIndex<br \/>\n&#8220;`<\/p>\n<p>\u9019\u80fd\u5e6b\u52a9\u6211\u5011\u4e86\u89e3\u7528\u6236\u7576\u524d\u7684\u9078\u64c7\u3002<\/p>\n<h2>\u70ba\u5206\u6bb5\u63a7\u4ef6\u6dfb\u52a0\u4e8b\u4ef6\u8655\u7406\u65b9\u6cd5<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `addTarget(_:action:for:)` \u65b9\u6cd5\u4f86\u70ba\u5206\u6bb5\u63a7\u4ef6\u6dfb\u52a0\u4e8b\u4ef6\u8655\u7406\u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nsegmentedControl.addTarget(self, action: #selector(handleSegmentChange), for: .valueChanged)<br \/>\n&#8220;`<\/p>\n<p>\u5728 `handleSegmentChange` \u65b9\u6cd5\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u7372\u53d6\u5206\u6bb5\u63a7\u4ef6\u7684\u7576\u524d\u9078\u4e2d\u7d22\u5f15\uff0c\u4e26\u505a\u51fa\u76f8\u61c9\u7684\u8655\u7406\uff1a<\/p>\n<p>&#8220;`swift<br \/>\n@objc func handleSegmentChange() {<br \/>\n    let selectedIndex = segmentedControl.selectedSegmentIndex<br \/>\n    \/\/ \u6839\u64da\u9078\u4e2d\u7d22\u5f15\u57f7\u884c\u76f8\u61c9\u64cd\u4f5c<br \/>\n    switch selectedIndex {<br \/>\n    case 0:<br \/>\n        print(&#8220;\u9078\u64c7\u4e86\u9078\u9805 1&#8221;)<br \/>\n    case 1:<br \/>\n        print(&#8220;\u9078\u64c7\u4e86\u9078\u9805 2&#8221;)<br \/>\n    case 2:<br \/>\n        print(&#8220;\u9078\u64c7\u4e86\u9078\u9805 3&#8221;)<br \/>\n    default:<br \/>\n        break<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u5efa\u8b70<\/h2>\n<p>&#8211; \u78ba\u4fdd\u5728\u8996\u5716\u52a0\u8f09\u5f8c\u6dfb\u52a0 UISegmentedControl \u5230\u8996\u5716\u4e2d\uff0c\u5426\u5247\u53ef\u80fd\u7121\u6cd5\u986f\u793a\u3002<br \/>\n&#8211; \u6aa2\u67e5\u6a19\u984c\u662f\u5426\u6b63\u78ba\u63d2\u5165\uff0c\u4e26\u78ba\u4fdd\u6c92\u6709\u91cd\u8907\u7684\u9078\u9805\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u53ef\u4ee5\u5c07 UISegmentedControl \u8207\u5176\u4ed6 UI \u5143\u7d20\uff08\u5982 UILabel \u6216 UIImageView\uff09\u7d44\u5408\u4f7f\u7528\uff0c\u6839\u64da\u7528\u6236\u9078\u64c7\u52d5\u614b\u66f4\u65b0\u986f\u793a\u5167\u5bb9\u3002\u4f8b\u5982\uff0c\u5728\u9078\u64c7\u4e0d\u540c\u9078\u9805\u6642\u986f\u793a\u4e0d\u540c\u7684\u5716\u7247\u6216\u6587\u5b57\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u4e2d\u7684 UISegmentedControl \u5be6\u73fe\u5206\u6bb5\u63a7\u4ef6\uff0c\u6db5\u84cb\u4e86\u5275\u5efa\u63a7\u4ef6\u3001\u8a2d\u7f6e\u6a19\u984c\u3001\u7372\u53d6\u9078\u4e2d\u7d22\u5f15\u53ca\u4e8b\u4ef6\u8655\u7406\u7b49\u57fa\u672c\u6b65\u9a5f\u3002\u901a\u904e\u5b78\u7fd2\u9019\u4e9b\u5167\u5bb9\uff0c\u4f60\u61c9\u8a72\u80fd\u5920\u9748\u6d3b\u5730\u4f7f\u7528 UISegmentedControl\uff0c\u63d0\u5347\u4f60\u7684 iOS \u958b\u767c\u6280\u80fd\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: UISegmentedControl \u53ef\u4ee5\u81ea\u5b9a\u7fa9\u6a23\u5f0f\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0c\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e\u80cc\u666f\u984f\u8272\u3001\u9078\u4e2d\u984f\u8272\u4f86\u81ea\u5b9a\u7fa9 UISegmentedControl \u7684\u6a23\u5f0f\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728 UISegmentedControl \u4e2d\u4f7f\u7528\u5716\u7247\u800c\u4e0d\u662f\u6587\u5b57\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `insertSegment(with:at:animated:)` \u65b9\u6cd5\u63d2\u5165 UIImage\uff0c\u5f9e\u800c\u5728\u5206\u6bb5\u63a7\u4ef6\u4e2d\u986f\u793a\u5716\u7247\u3002<\/p>\n<p>**Q3: UISegmentedControl \u6709\u4ec0\u9ebc\u66ff\u4ee3\u65b9\u6848\uff1f**<br \/>\nA3: \u5982\u679c\u9700\u8981\u66f4\u8907\u96dc\u7684\u9078\u64c7\u754c\u9762\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 UICollectionView \u6216\u81ea\u5b9a\u7fa9 UIView\u3002\u9019\u4e9b\u7d44\u4ef6\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u9748\u6d3b\u6027\u548c\u81ea\u5b9a\u7fa9\u9078\u9805\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u5206\u6bb5\u63a7\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528UISegmentedControl\u7c7b\u5b9e\u73b0\u5206\u6bb5\u63a7\u4ef6\u3001\u8a2d\u7f6e\u5206\u6bb5\u63a7\u4ef6\u7684\u6a19\u984c\u3001\u7372\u53d6\u5206\u6bb5\u63a7\u4ef6\u7684\u7576\u524d\u9078\u4e2d\u7d22\u5f15\u3001\u70ba\u5206\u6bb5\u63a7\u4ef6\u6dfb\u52a0\u4e8b\u4ef6\u8655\u7406\u65b9\u6cd5\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-3979","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-12b","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3979","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=3979"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3979\/revisions"}],"predecessor-version":[{"id":3980,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3979\/revisions\/3980"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}