{"id":7631,"date":"2025-05-29T14:49:21","date_gmt":"2025-06-05T07:25:21","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7631"},"modified":"2025-06-05T14:49:21","modified_gmt":"2025-06-05T07:25:21","slug":"https-badgameshow-com-steven-467","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-467\/","title":{"rendered":"2025 \u6700\u65b0 Swift UISegmentedControl \u4f7f\u7528\u6559\u5b78 \u2013 \u5feb\u901f\u638c\u63e1\u5206\u6bb5\u63a7\u5236\u9805\u7684\u61c9\u7528"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UISegmentedControl, \u5b78\u7fd2\u6559\u5b78, iOS \u958b\u767c, UI \u5143\u4ef6\"><\/p>\n<p>Swift \u662f\u4e00\u7a2e\u5728 iOS \u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\u5ee3\u53d7\u6b61\u8fce\u7684\u7a0b\u5f0f\u8a9e\u8a00\u3002\u900f\u904e\u5b83\uff0c\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u69cb\u5efa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u958b\u767c UISegmentedControl\uff0c\u9019\u662f\u4e00\u500b\u7528\u65bc\u63d0\u4f9b\u591a\u9078\u9805\u7684 UI \u5143\u4ef6\uff0c\u8b93\u4f7f\u7528\u8005\u80fd\u5920\u8f15\u9b06\u5207\u63db\u61c9\u7528\u7a0b\u5f0f\u4e2d\u7684\u4e0d\u540c\u529f\u80fd\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f UISegmentedControl\uff1f<\/h2>\n<p>UISegmentedControl \u662f iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\u5e38\u7528\u7684 UI \u5143\u4ef6\uff0c\u4e3b\u8981\u7528\u65bc\u63d0\u4f9b\u591a\u500b\u9078\u9805\u8b93\u4f7f\u7528\u8005\u9032\u884c\u9078\u64c7\u3002\u8209\u4f8b\u4f86\u8aaa\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u5207\u63db\u300c\u641c\u5c0b\u300d\u548c\u300c\u8a2d\u5b9a\u300d\u7b49\u529f\u80fd\uff0c\u5f9e\u800c\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u958b\u767c UISegmentedControl\uff1f<\/h2>\n<p>\u4f7f\u7528 Swift \u958b\u767c UISegmentedControl \u76f8\u5c0d\u7c21\u55ae\uff0c\u53ea\u9700\u5e7e\u884c\u7a0b\u5f0f\u78bc\u5373\u53ef\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u5efa\u7acb UISegmentedControl \u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3>1. \u5efa\u7acb UISegmentedControl \u7269\u4ef6<\/h3>\n<pre><code class=\"language-swift line-numbers\">let segmentedControl = UISegmentedControl()<\/code><\/pre>\n<h3>2. \u8a2d\u5b9a\u6a23\u5f0f<\/h3>\n<p>\u6211\u5011\u53ef\u4ee5\u8a2d\u7f6e UISegmentedControl \u7684\u6309\u9215\u6587\u5b57\u3001\u984f\u8272\u548c\u5927\u5c0f\u7b49\u5c6c\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">segmentedControl.insertSegment(withTitle: \"Search\", at: 0, animated: false)\nsegmentedControl.insertSegment(withTitle: \"Settings\", at: 1, animated: false)\nsegmentedControl.tintColor = UIColor.black\nsegmentedControl.frame = CGRect(x: 0, y: 0, width: 200, height: 30)<\/code><\/pre>\n<h3>3. \u8a2d\u5b9a\u52d5\u4f5c<\/h3>\n<p>\u7576\u4f7f\u7528\u8005\u9078\u64c7\u4e0d\u540c\u7684\u6309\u9215\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u6dfb\u52a0\u76f8\u61c9\u7684\u52d5\u4f5c\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .valueChanged)<\/code><\/pre>\n<h3>4. \u5be6\u4f5c\u52d5\u4f5c\u51fd\u5f0f<\/h3>\n<p>\u5728\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u7576\u4f7f\u7528\u8005\u6309\u4e0b\u6309\u9215\u6642\uff0c\u5c07\u6703\u8abf\u7528 <code>segmentedControlValueChanged<\/code> \u51fd\u5f0f\uff0c\u4ee5\u4e0b\u662f\u5177\u9ad4\u5be6\u4f5c\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {\n    switch sender.selectedSegmentIndex {\n    case 0:\n        \/\/ \u57f7\u884c\u641c\u5c0b\u52d5\u4f5c\n        print(\"\u57f7\u884c\u641c\u5c0b\")\n    case 1:\n        \/\/ \u57f7\u884c\u8a2d\u5b9a\u52d5\u4f5c\n        print(\"\u57f7\u884c\u8a2d\u5b9a\")\n    default:\n        break\n    }\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u958b\u767c UISegmentedControl \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u6309\u9215\u7121\u6cd5\u56de\u61c9\uff1a<\/strong>\u78ba\u4fdd\u60a8\u5df2\u7d93\u5c07 UISegmentedControl \u7684 target \u548c action \u6b63\u78ba\u8a2d\u7f6e\u3002<\/li>\n<li><strong>\u6a23\u5f0f\u672a\u6b63\u78ba\u986f\u793a\uff1a<\/strong>\u6aa2\u67e5 UISegmentedControl \u7684 frame \u548c tintColor \u662f\u5426\u8a2d\u7f6e\u6b63\u78ba\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UISegmentedControl \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u5207\u63db\u529f\u80fd\uff0c\u9084\u80fd\u7528\u65bc\u7be9\u9078\u8cc7\u6599\u6216\u5207\u63db\u986f\u793a\u6a21\u5f0f\u3002\u60a8\u53ef\u4ee5\u6839\u64da\u61c9\u7528\u9700\u6c42\u9032\u4e00\u6b65\u64f4\u5c55\u5176\u529f\u80fd\uff0c\u4f8b\u5982\u8207 TableView \u6216 CollectionView \u6574\u5408\uff0c\u5be6\u73fe\u66f4\u8907\u96dc\u7684 UI \u884c\u70ba\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5be6\u73fe UISegmentedControl\uff0c\u8b93\u60a8\u80fd\u5920\u5feb\u901f\u4e0a\u624b iOS \u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u3002\u901a\u904e\u7c21\u55ae\u7684\u8a2d\u5b9a\uff0c\u60a8\u53ef\u4ee5\u8b93\u4f7f\u7528\u8005\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u8f15\u9b06\u9078\u64c7\u4e0d\u540c\u7684\u9078\u9805\uff0c\u5f9e\u800c\u63d0\u5347\u5176\u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5206\u6bb5\u63a7\u5236\u9805UISegmentedControl-\ud83d\udd25\u5b78\u7fd2\u6559\u5b78-\u2013-\u638c\u63e1\u5982\u4f55\u4f7f\u7528_1675478355.417216.webp\" alt=\"Swift \u5206\u6bb5\u63a7\u5236\u9805(UISegmentedControl) \u4f7f\u7528\u7bc4\u4f8b\" title=\"Swift UISegmentedControl \u4f7f\u7528\u7bc4\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5be6\u4f5c\u5206\u6bb5\u63a7\u5236\u9805\u7684\u529f\u80fd\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UISegmentedControl \u5982\u4f55\u8207\u5176\u4ed6 UI \u5143\u4ef6\u7d50\u5408\u4f7f\u7528\uff1f<\/h3>\n<p>A1: UISegmentedControl \u53ef\u4ee5\u8f15\u9b06\u8207 TableView \u6216 CollectionView \u7d50\u5408\u4f7f\u7528\uff0c\u6839\u64da\u9078\u64c7\u7684\u4e0d\u540c\u9078\u9805\u4f86\u66f4\u65b0\u986f\u793a\u7684\u8cc7\u6599\u3002<\/p>\n<h3>Q2: UISegmentedControl \u652f\u63f4\u81ea\u5b9a\u7fa9\u6a23\u5f0f\u55ce\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5c6c\u6027\u5982 <code>tintColor<\/code> \u548c <code>backgroundColor<\/code> \u4f86\u81ea\u5b9a\u7fa9 UISegmentedControl \u7684\u5916\u89c0\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2dUISegmentedControl\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u4f7f\u7528UISegmentedControl\uff0c\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u66f4\u52a0\u5b8c\u5584\u3002<\/p>\n","protected":false},"author":1,"featured_media":7629,"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-7631","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-\u5206\u6bb5\u63a7\u5236\u9805UISegmentedControl-\ud83d\udd25\u5b78\u7fd2\u6559\u5b78-\u2013-\u638c\u63e1\u5982\u4f55\u4f7f\u7528_1675478355.417216.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Z5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7631","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=7631"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7631\/revisions"}],"predecessor-version":[{"id":13206,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7631\/revisions\/13206"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7629"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}