{"id":7394,"date":"2025-05-31T12:35:30","date_gmt":"2025-06-05T05:27:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7394"},"modified":"2025-06-05T12:35:30","modified_gmt":"2025-06-05T05:27:29","slug":"https-badgameshow-com-steven-368","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-368\/","title":{"rendered":"Swift UISwitch \u5b8c\u6574\u6559\u5b78\uff1a\u5f9e\u958b\u95dc\u5207\u63db\u958b\u555f\/\u95dc\u9589\u529f\u80fd\u5230\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UISwitch, \u958b\u95dc\u5207\u63db, iOS \u958b\u767c, Swift \u6559\u5b78\"><\/p>\n<p>Swift \u662f\u4e00\u7a2e\u7c21\u55ae\u6613\u5b78\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u8c50\u5bcc\u7684 iOS \u61c9\u7528\u7a0b\u5f0f\u3002\u5728 Swift \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 <strong>UISwitch<\/strong> \u63a7\u5236\u5143\u4ef6\u4f86\u958b\u555f\u6216\u95dc\u9589\u67d0\u4e9b\u529f\u80fd\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5be6\u73fe UISwitch \u7684\u958b\u95dc\u5207\u63db\u529f\u80fd\uff0c\u5305\u62ec\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4f7f\u7528 Swift \u5be6\u73fe UISwitch \u958b\u95dc\u5207\u63db\u958b\u555f\/\u95dc\u9589<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 UISwitch \u63a7\u5236\u5143\u4ef6\u4f86\u5be6\u73fe\u958b\u95dc\u5207\u63db\u7684\u529f\u80fd\u3002UISwitch \u6709\u5169\u500b\u72c0\u614b\uff1a\u958b\u555f\u548c\u95dc\u9589\u3002\u4ee5\u4e0b\u662f\u5be6\u73fe UISwitch \u958b\u95dc\u5207\u63db\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3>\u6b65\u9a5f 1: \u5efa\u7acb UISwitch \u63a7\u5236\u5143\u4ef6<\/h3>\n<pre><code class=\"language-swift line-numbers\">let switchControl = UISwitch()\nswitchControl.isOn = true \/\/ \u9810\u8a2d\u70ba\u958b\u555f\u72c0\u614b\nswitchControl.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 2: \u5be6\u4f5c switchValueDidChange \u65b9\u6cd5<\/h3>\n<p>\u7576\u7528\u6236\u5207\u63db UISwitch \u6642\uff0c\u6703\u89f8\u767c\u9019\u500b\u65b9\u6cd5\u3002\u6211\u5011\u53ef\u4ee5\u6839\u64da UISwitch \u7684\u72c0\u614b\u4f86\u57f7\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func switchValueDidChange(_ sender: UISwitch) {\n    if sender.isOn {\n        \/\/ \u958b\u555f\u529f\u80fd\u7684\u8655\u7406\u908f\u8f2f\n        print(\"\u529f\u80fd\u5df2\u958b\u555f\")\n    } else {\n        \/\/ \u95dc\u9589\u529f\u80fd\u7684\u8655\u7406\u908f\u8f2f\n        print(\"\u529f\u80fd\u5df2\u95dc\u9589\")\n    }\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 3: \u5c07 UISwitch \u6dfb\u52a0\u5230\u8996\u5716\u4e2d<\/h3>\n<p>\u6700\u5f8c\uff0c\u5c07 UISwitch \u63a7\u5236\u5143\u4ef6\u6dfb\u52a0\u5230\u4f60\u7684\u8996\u5716\u4e2d\uff0c\u78ba\u4fdd\u5b83\u80fd\u5920\u986f\u793a\u4e26\u6b63\u5e38\u904b\u4f5c\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">view.addSubview(switchControl)\nswitchControl.center = view.center \/\/ \u8a2d\u7f6e UISwitch \u7684\u4f4d\u7f6e\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c UISwitch \u7121\u6cd5\u6b63\u5e38\u5de5\u4f5c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd <code>addTarget<\/code> \u65b9\u6cd5\u5df2\u6b63\u78ba\u8a2d\u7f6e\uff0c\u4e26\u4e14 <code>switchValueDidChange<\/code> \u65b9\u6cd5\u662f <code>@objc<\/code> \u6a19\u8a18\u7684\u3002<\/li>\n<li>\u78ba\u8a8d UISwitch \u5df2\u7d93\u6dfb\u52a0\u5230\u8996\u5716\u4e2d\uff0c\u4e26\u4e14\u5176\u4f4d\u7f6e\u5728\u53ef\u898b\u7bc4\u570d\u5167\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UISwitch \u9664\u4e86\u7528\u65bc\u958b\u95dc\u529f\u80fd\u5916\uff0c\u9084\u53ef\u4ee5\u7d50\u5408\u5176\u4ed6 UI \u5143\u7d20\u4f7f\u7528\uff0c\u4f8b\u5982\u8207 <strong>UILabel<\/strong> \u7d44\u5408\uff0c\u986f\u793a\u958b\u95dc\u72c0\u614b\uff0c\u6216\u8005\u5728\u8868\u55ae\u4e2d\u4f7f\u7528\u4f86\u63a7\u5236\u7279\u5b9a\u9078\u9805\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UISwitch\u958b\u95dc\u5207\u63db\u958b\u555f-\u95dc\u9589_1675475210.258537.webp\" alt=\"Swift UISwitch\u958b\u95dc\u5207\u63db\u958b\u555f\/\u95dc\u9589\" title=\"Swift UISwitch, \u958b\u95dc\u5207\u63db, \u958b\u555f\/\u95dc\u9589, \u63a7\u5236\u529f\u80fd, \u61c9\u7528\u7a0b\u5e8f\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UISwitch \u80fd\u5426\u8207\u5176\u4ed6 UI \u63a7\u5236\u5143\u4ef6\u4e00\u8d77\u4f7f\u7528\uff1f<\/h3>\n<p>\u662f\u7684\uff0cUISwitch \u53ef\u4ee5\u8207 UILabel\u3001UIButton \u7b49\u5176\u4ed6 UI \u63a7\u5236\u5143\u4ef6\u4e00\u8d77\u4f7f\u7528\uff0c\u4f86\u63d0\u4f9b\u66f4\u8c50\u5bcc\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h3>2. \u5982\u4f55\u6539\u8b8a UISwitch \u7684\u984f\u8272\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 UISwitch \u7684 <code>onTintColor<\/code> \u548c <code>tintColor<\/code> \u5c6c\u6027\u4f86\u81ea\u5b9a\u7fa9\u958b\u95dc\u7684\u984f\u8272\u3002<\/p>\n<h3>3. UISwitch \u652f\u63f4\u7121\u969c\u7919\u529f\u80fd\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0cUISwitch \u652f\u63f4\u7121\u969c\u7919\u529f\u80fd\uff0c\u4e26\u4e14\u53ef\u4ee5\u901a\u904e VoiceOver \u7b49\u8f14\u52a9\u6280\u8853\u9032\u884c\u4f7f\u7528\u3002<\/p>\n<p>\u901a\u904e\u9019\u7bc7\u6587\u7ae0\uff0c\u6211\u5011\u5b78\u6703\u4e86\u5982\u4f55\u5728 iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\u5be6\u73fe UISwitch \u958b\u95dc\u5207\u63db\u7684\u529f\u80fd\uff0c\u4e26\u638c\u63e1\u4e86\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\u3002\u5e0c\u671b\u9019\u4e9b\u8cc7\u8a0a\u80fd\u5e6b\u52a9\u60a8\u63d0\u5347\u958b\u767c\u6280\u8853\uff0c\u5275\u9020\u51fa\u66f4\u597d\u7684\u61c9\u7528\u7a0b\u5f0f\u9ad4\u9a57\uff01<\/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\u7684UISwitch\u958b\u95dc\u5207\u63db\u958b\u555f\/\u95dc\u9589\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u63a7\u5236\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002\u6211\u5011\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528UISwitch\u958b\u95dc\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63a7\u5236\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":7393,"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-7394","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-UISwitch\u958b\u95dc\u5207\u63db\u958b\u555f-\u95dc\u9589_1675475210.258537.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Vg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7394","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=7394"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7394\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7393"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}