{"id":6983,"date":"2025-05-30T13:39:58","date_gmt":"2025-06-05T06:04:57","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6983"},"modified":"2025-06-05T13:39:58","modified_gmt":"2025-06-05T06:04:57","slug":"https-badgameshow-com-steven-167","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-167\/","title":{"rendered":"Swift UISwitch \u6700\u65b0\u5165\u9580\u6559\u7a0b\uff1a\u5168\u9762\u638c\u63e1\u5207\u63db\u958b\u95dc\u7684\u4f7f\u7528\u6280\u5de7"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UISwitch, \u6700\u65b0\u5165\u9580\u6559\u7a0b, \u5207\u63db\u958b\u95dc, Swift \u6559\u5b78, iOS \u958b\u767c\"><\/p>\n<h1>Swift UISwitch \u6700\u65b0\u5165\u9580\u6559\u7a0b\uff1a\u5168\u9762\u638c\u63e1\u5207\u63db\u958b\u95dc\u7684\u4f7f\u7528\u6280\u5de7<\/h1>\n<p>UISwitch \u662f iOS \u958b\u767c\u4e2d\u975e\u5e38\u5e38\u7528\u7684\u63a7\u5236\u5143\u4ef6\uff0c\u4e3b\u8981\u7528\u65bc\u63a7\u5236\u4e00\u500b\u503c\u7684\u958b\u95dc\u72c0\u614b\uff0c\u4f8b\u5982\u958b\u555f\u6216\u95dc\u9589\u67d0\u500b\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 UISwitch\uff0c\u4e26\u63d0\u4f9b 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u638c\u63e1\u9019\u4e00\u5143\u4ef6\u7684\u4f7f\u7528\u3002<\/p>\n<h2>\u5275\u5efa UISwitch<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b UISwitch \u4f86\u63a7\u5236\u4e00\u500b\u503c\u7684\u958b\u95dc\u72c0\u614b\u3002\u4ee5\u4e0b\u4ee3\u78bc\u793a\u7bc4\u4e86\u5982\u4f55\u5275\u5efa UISwitch \u7684\u5be6\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let switchControl = UISwitch()\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5275\u5efa\u4e00\u500b UISwitch \u5be6\u4f8b\u4e26\u5c07\u5176\u6307\u6d3e\u7d66\u8b8a\u91cf <code>switchControl<\/code>\u3002<\/p>\n<h2>\u8a2d\u5b9a UISwitch \u5916\u89c0<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u81ea\u8a02 UISwitch \u7684\u5916\u89c0\uff0c\u4ee5\u7b26\u5408\u61c9\u7528\u7a0b\u5e8f\u7684\u8a2d\u8a08\u98a8\u683c\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u8a2d\u5b9a UISwitch \u7684\u984f\u8272\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">switchControl.tintColor = UIColor.red \/\/ \u958b\u95dc\u672a\u6253\u958b\u6642\u7684\u984f\u8272\nswitchControl.onTintColor = UIColor.green \/\/ \u958b\u95dc\u6253\u958b\u6642\u7684\u984f\u8272\nswitchControl.thumbTintColor = UIColor.blue \/\/ \u958b\u95dc\u7684\u5713\u5f62\u6309\u9215\u984f\u8272\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07 UISwitch \u7684\u5916\u89c0\u8a2d\u5b9a\u70ba\u7d05\u8272\u3001\u7da0\u8272\u548c\u85cd\u8272\uff0c\u6709\u6548\u63d0\u5347\u4e86\u7528\u6236\u754c\u9762\u7684\u53ef\u8996\u6027\u3002<\/p>\n<h2>\u8a2d\u5b9a UISwitch \u884c\u70ba<\/h2>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u8a2d\u5b9a UISwitch \u7684\u521d\u59cb\u884c\u70ba\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">switchControl.isOn = true \/\/ \u8a2d\u5b9a\u958b\u95dc\u521d\u59cb\u72c0\u614b\u70ba\u958b\u555f\nswitchControl.isEnabled = true \/\/ \u8a2d\u5b9a\u958b\u95dc\u53ef\u7528\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07 UISwitch \u7684\u958b\u95dc\u72c0\u614b\u8a2d\u5b9a\u70ba\u958b\u555f\uff0c\u4e26\u78ba\u4fdd\u7528\u6236\u53ef\u4ee5\u64cd\u4f5c\u5b83\u3002<\/p>\n<h2>\u6dfb\u52a0 UISwitch \u5230\u754c\u9762<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5c07 UISwitch \u6dfb\u52a0\u5230\u7528\u6236\u754c\u9762\u4e2d\uff0c\u4ee5\u4fbf\u7528\u6236\u53ef\u4ee5\u770b\u5230\u4e26\u64cd\u4f5c\u5b83\u3002\u4ee5\u4e0b\u662f\u5c07 UISwitch \u6dfb\u52a0\u5230\u8996\u5716\u7684\u4ee3\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">view.addSubview(switchControl) \/\/ \u5c07 UISwitch \u6dfb\u52a0\u5230\u7576\u524d\u8996\u5716\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u6703\u5c07 UISwitch \u6dfb\u52a0\u5230\u7576\u524d\u8996\u5716\uff0c\u4e26\u4f7f\u5176\u53ef\u898b\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 UISwitch \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\u958b\u95dc\u7121\u6cd5\u6b63\u5e38\u986f\u793a\u6216\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6392\u9664\u932f\u8aa4\u7684\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd UISwitch \u5df2\u6b63\u78ba\u6dfb\u52a0\u5230\u8996\u5716\u4e2d\u3002<\/li>\n<li>\u6aa2\u67e5 UISwitch \u7684 <code>isEnabled<\/code> \u5c6c\u6027\u662f\u5426\u8a2d\u7f6e\u70ba <code>true<\/code>\u3002<\/li>\n<li>\u78ba\u8a8d UISwitch \u7684\u7d04\u675f\uff08Auto Layout constraints\uff09\u662f\u5426\u6b63\u78ba\uff0c\u4ee5\u4fbf\u5728\u754c\u9762\u4e2d\u6b63\u78ba\u986f\u793a\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UISwitch \u53ef\u4ee5\u8207\u5176\u4ed6 UI \u5143\u4ef6\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982 UILabel \u6216 UIButton\uff0c\u4ee5\u53cd\u6620\u958b\u95dc\u72c0\u614b\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728 UISwitch \u7684 <code>ValueChanged<\/code> \u4e8b\u4ef6\u4e2d\u66f4\u65b0 UILabel \u6587\u5b57\uff0c\u986f\u793a\u7576\u524d\u72c0\u614b\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">switchControl.addTarget(self, action: #selector(switchChanged(_:)), for: .valueChanged)\n\n@objc func switchChanged(_ sender: UISwitch) {\n    let status = sender.isOn ? \"\u958b\u555f\" : \"\u95dc\u9589\"\n    print(\"\u958b\u95dc\u72c0\u614b\uff1a\\(status)\")\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u6703\u5728\u958b\u95dc\u72c0\u614b\u6539\u8b8a\u6642\uff0c\u900f\u904e console \u8f38\u51fa\u7576\u524d\u72c0\u614b\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 UISwitch \u4f86\u63a7\u5236\u4e00\u500b\u503c\u7684\u958b\u95dc\u72c0\u614b\u3002\u5f9e\u5275\u5efa UISwitch\u3001\u8a2d\u5b9a\u5916\u89c0\u8207\u884c\u70ba\uff0c\u5230\u5c07\u5176\u6dfb\u52a0\u5230\u754c\u9762\uff0c\u9019\u4e9b\u90fd\u662f\u958b\u767c\u8005\u5728 iOS \u61c9\u7528\u4e2d\u5e38\u7528\u7684\u64cd\u4f5c\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u638c\u63e1 UISwitch \u7684\u4f7f\u7528\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UISwitch-\u5165\u9580\u6559\u7a0b-\u5207\u63db\u958b\u95dc-\ud83d\udca1_1675399891.28658.webp\" alt=\"Swift UISwitch \u5165\u9580\u6559\u7a0b \u5207\u63db\u958b\u95dc \ud83d\udca1\" title=\"Swift UISwitch, \u958b\u95dc\u63a7\u5236, App\u63a7\u5236, UISwitch, Swift\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q: UISwitch \u53ef\u4ee5\u8207\u54ea\u4e9b\u5176\u4ed6 UI \u5143\u4ef6\u4e00\u8d77\u4f7f\u7528\uff1f<\/h3>\n<p>A: UISwitch \u53ef\u4ee5\u8207 UILabel\u3001UIButton \u7b49\u5176\u4ed6 UI \u5143\u4ef6\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u986f\u793a\u6216\u63a7\u5236\u5176\u4ed6\u529f\u80fd\u3002<\/p>\n<h3>Q: \u5982\u4f55\u5728 Swift \u4e2d\u76e3\u807d UISwitch \u7684\u8b8a\u5316\uff1f<\/h3>\n<p>A: \u53ef\u4ee5\u4f7f\u7528 <code>addTarget(_:action:for:)<\/code> \u65b9\u6cd5\u4f86\u76e3\u807d UISwitch \u7684 <code>ValueChanged<\/code> \u4e8b\u4ef6\uff0c\u4e26\u57f7\u884c\u76f8\u61c9\u7684\u52d5\u4f5c\u3002<\/p>\n<h3>Q: UISwitch \u7684\u984f\u8272\u8a2d\u5b9a\u6709\u4f55\u6700\u4f73\u5be6\u8e10\uff1f<\/h3>\n<p>A: \u78ba\u4fdd UISwitch \u7684\u984f\u8272\u8207\u61c9\u7528\u7a0b\u5e8f\u7684\u4e3b\u984c\u4e00\u81f4\uff0c\u4e26\u8003\u616e\u5230\u53ef\u5b58\u53d6\u6027\uff0c\u4f7f\u7528\u9ad8\u5c0d\u6bd4\u5ea6\u7684\u984f\u8272\u642d\u914d\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2dUISwitch\u7684\u958b\u95dc\u63a7\u5236\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528UISwitch\u4f86\u63a7\u5236\u4f60\u7684App\uff0c\u8b93\u4f60\u7684App\u66f4\u52a0\u5b8c\u5584\u3002<\/p>\n","protected":false},"author":1,"featured_media":6981,"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-6983","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-\u5165\u9580\u6559\u7a0b-\u5207\u63db\u958b\u95dc-\ud83d\udca1_1675399891.28658.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1OD","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6983","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=6983"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6983\/revisions"}],"predecessor-version":[{"id":13332,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6983\/revisions\/13332"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6981"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}