{"id":6652,"date":"2025-05-30T12:21:52","date_gmt":"2025-06-05T04:34:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/uncategorized\/https-badgameshow-com-steven-4\/"},"modified":"2025-06-05T12:21:52","modified_gmt":"2025-06-05T04:34:51","slug":"https-badgameshow-com-steven-4","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-4\/","title":{"rendered":"Swift UISegmentedControl \u5b8c\u6574\u6559\u5b78\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UISegmentedControl, iOS, macOS, Swift \u6559\u5b78\"><\/p>\n<h2>\u7c21\u4ecb<\/h2>\n<p>Swift UISegmentedControl \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u63a7\u5236\u5668\uff0c\u53ef\u5728 iOS \u548c macOS \u61c9\u7528\u4e2d\u5efa\u7acb\u5206\u6bb5\u63a7\u5236\u5668\u3002\u5b83\u5141\u8a31\u7528\u6236\u5728\u4e0d\u540c\u7684\u9078\u9805\u4e4b\u9593\u8f15\u9b06\u5207\u63db\uff0c\u63d0\u5347\u61c9\u7528\u7684\u4e92\u52d5\u6027\u548c\u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n<h2>\u4f7f\u7528 Swift \u5efa\u7acb UISegmentedControl<\/h2>\n<p>\u4f7f\u7528 Swift \u5efa\u7acb UISegmentedControl \u975e\u5e38\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u7a0b\u5f0f\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let segmentedControl = UISegmentedControl(items: [\"\u9078\u9805 1\", \"\u9078\u9805 2\", \"\u9078\u9805 3\"])<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u5efa\u7acb\u4e00\u500b UISegmentedControl\uff0c\u4e26\u5305\u542b\u4e09\u500b\u9078\u9805\uff1a\u300c\u9078\u9805 1\u300d\u3001\u300c\u9078\u9805 2\u300d\u548c\u300c\u9078\u9805 3\u300d\u3002<\/p>\n<h2>\u8a2d\u5b9a UISegmentedControl \u7684\u9078\u9805<\/h2>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u8a2d\u5b9a UISegmentedControl \u7684\u9810\u8a2d\u9078\u9805\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">segmentedControl.selectedSegmentIndex = 0<\/code><\/pre>\n<p>\u9019\u884c\u7a0b\u5f0f\u78bc\u5c07\u9810\u8a2d\u9078\u9805\u8a2d\u70ba\u300c\u9078\u9805 1\u300d\uff0c\u5373\u7d22\u5f15\u503c\u70ba 0 \u7684\u9078\u9805\u3002<\/p>\n<h2>\u8a2d\u5b9a UISegmentedControl \u7684\u5916\u89c0<\/h2>\n<p>\u8981\u81ea\u5b9a\u7fa9 UISegmentedControl \u7684\u5916\u89c0\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">segmentedControl.tintColor = UIColor.red<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07 UISegmentedControl \u7684\u4e3b\u8272\u8abf\u8a2d\u70ba\u7d05\u8272\u3002\u60a8\u53ef\u4ee5\u6839\u64da\u61c9\u7528\u7684\u4e3b\u984c\u9078\u64c7\u4e0d\u540c\u984f\u8272\u3002<\/p>\n<h2>\u8a2d\u5b9a UISegmentedControl \u7684\u52d5\u4f5c<\/h2>\n<p>\u70ba\u4e86\u8655\u7406\u7528\u6236\u7684\u9078\u64c7\uff0c\u60a8\u9700\u8981\u8a2d\u5b9a UISegmentedControl \u7684\u52d5\u4f5c\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">segmentedControl.addTarget(self, action: #selector(handleSegmentChange), for: .valueChanged)<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u5728\u7528\u6236\u5207\u63db\u9078\u9805\u6642\uff0c\u547c\u53eb <code>handleSegmentChange<\/code> \u65b9\u6cd5\u3002\u8acb\u78ba\u4fdd\u60a8\u5df2\u6b63\u78ba\u5be6\u4f5c\u8a72\u65b9\u6cd5\u4f86\u8655\u7406\u9078\u9805\u8b8a\u52d5\u3002<\/p>\n<h2>\u5b8c\u6574\u7684 handleSegmentChange \u65b9\u6cd5\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 <code>handleSegmentChange<\/code> \u65b9\u6cd5\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func handleSegmentChange(segmentedControl: UISegmentedControl) {\n    switch segmentedControl.selectedSegmentIndex {\n    case 0:\n        print(\"\u9078\u64c7\u4e86\u9078\u9805 1\")\n    case 1:\n        print(\"\u9078\u64c7\u4e86\u9078\u9805 2\")\n    case 2:\n        print(\"\u9078\u64c7\u4e86\u9078\u9805 3\")\n    default:\n        break\n    }\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c UISegmentedControl \u7121\u6cd5\u6b63\u5e38\u904b\u4f5c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u500b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u60a8\u5df2\u5c07 UISegmentedControl \u6dfb\u52a0\u5230\u8996\u5716\u4e2d\uff0c\u4e26\u6b63\u78ba\u8a2d\u7f6e\u7d04\u675f\u3002<\/li>\n<li>\u6aa2\u67e5\u76ee\u6a19\u8207\u884c\u70ba\u662f\u5426\u5df2\u6b63\u78ba\u8a2d\u5b9a\u3002<\/li>\n<li>\u78ba\u4fdd\u60a8\u5df2\u5be6\u4f5c <code>handleSegmentChange<\/code> \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UISegmentedControl \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u9078\u64c7\u4e0d\u540c\u9078\u9805\uff0c\u9084\u53ef\u4ee5\u7d50\u5408\u5176\u4ed6 UI \u5143\u7d20\uff0c\u5982 TableView \u6216 CollectionView\uff0c\u4f86\u986f\u793a\u4e0d\u540c\u985e\u578b\u7684\u5167\u5bb9\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 UISegmentedControl \u5207\u63db\u986f\u793a\u5217\u8868\u6216\u7db2\u683c\u8996\u5716\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift UISegmentedControl \u662f\u4e00\u500b\u5f37\u5927\u4e14\u6613\u65bc\u4f7f\u7528\u7684\u63a7\u5236\u5668\uff0c\u8b93\u60a8\u80fd\u5920\u5728 iOS \u548c macOS \u61c9\u7528\u4e2d\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6236\u9ad4\u9a57\u3002\u900f\u904e\u4e0a\u8ff0\u7684\u6559\u5b78\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u8f15\u9b06\u5730\u5be6\u4f5c UISegmentedControl\uff0c\u4e26\u81ea\u5b9a\u7fa9\u5176\u5916\u89c0\u8207\u884c\u70ba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UISegmentedControl-\ud83d\udcbb\u5206\u6bb5\u63a7\u5236\u5668\uff01_1675393127.692757.webp\" alt=\"Swift UISegmentedControl \ud83d\udcbb\u5206\u6bb5\u63a7\u5236\u5668\uff01\" title=\"Swift UISegmentedControl\u3001Swift \u5206\u6bb5\u63a7\u5236\u5668\u3001Swift \u529f\u80fd\u3001Swift \u61c9\u7528\u7a0b\u5e8f\u3001Swift \u9078\u9805\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UISegmentedControl \u53ef\u4ee5\u6709\u591a\u5c11\u500b\u9078\u9805\uff1f<\/h3>\n<p>A1: UISegmentedControl \u53ef\u4ee5\u5305\u542b\u4efb\u610f\u6578\u91cf\u7684\u9078\u9805\uff0c\u4f46\u904e\u591a\u7684\u9078\u9805\u53ef\u80fd\u6703\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\uff0c\u56e0\u6b64\u5efa\u8b70\u9650\u5236\u5728 3-5 \u500b\u9078\u9805\u4e4b\u5167\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5728 UISegmentedControl \u4e2d\u6dfb\u52a0\u5716\u50cf\uff1f<\/h3>\n<p>A2: \u60a8\u53ef\u4ee5\u4f7f\u7528 <code>UISegmentedControl.setImage(_:forSegmentAt:)<\/code> \u65b9\u6cd5\u70ba\u6bcf\u500b\u9078\u9805\u6dfb\u52a0\u5716\u50cf\u3002<\/p>\n<h3>Q3: UISegmentedControl \u662f\u5426\u652f\u6301\u52d5\u614b\u66f4\u65b0\u9078\u9805\uff1f<\/h3>\n<p>A3: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u904b\u884c\u6642\u4f7f\u7528 <code>insertSegment(withTitle:at:animated:)<\/code> \u6216 <code>removeSegment(at:animated:)<\/code> \u65b9\u6cd5\u4f86\u52d5\u614b\u6dfb\u52a0\u6216\u79fb\u9664\u9078\u9805\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2Swift\u4e2d\u7684UISegmentedControl\u5206\u6bb5\u63a7\u5236\u5668\uff0c\u53ef\u4ee5\u8b93\u4f60\u8f15\u9b06\u5730\u5728\u4e0d\u540c\u7684\u9078\u9805\u4e4b\u9593\u5207\u63db\uff0c\u4e26\u63a7\u5236\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u884c\u70ba\u3002<\/p>\n","protected":false},"author":1,"featured_media":6651,"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-6652","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-UISegmentedControl-\ud83d\udcbb\u5206\u6bb5\u63a7\u5236\u5668\uff01_1675393127.692757.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Ji","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6652","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=6652"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6652\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6651"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}