{"id":1139,"date":"2025-05-31T10:54:42","date_gmt":"2025-06-05T03:37:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=1139"},"modified":"2025-06-05T10:54:42","modified_gmt":"2025-06-05T03:37:42","slug":"swift-uibutton-%e6%95%99%e5%ad%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-uibutton-%e6%95%99%e5%ad%b8\/","title":{"rendered":"\ud83d\udd30 \u6700\u65b0\u7248 Swift UIButton \u4f7f\u7528\u6307\u5357 &#8211; 2025\u5e74"},"content":{"rendered":"<h2>\u7c21\u4ecb<\/h2>\n<p>\u6309\u9215\u662f iOS \u958b\u767c\u4e2d\u6700\u57fa\u790e\u7684 UI \u5143\u4ef6\u4e4b\u4e00\u3002\u4f7f\u7528\u8005\u53ef\u4ee5\u8a2d\u7f6e\u6587\u5b57\u6216\u5716\u7247\u8a2d\u8a08\u6309\u9215\uff0c\u4e26\u642d\u914d\u6240\u9700\u7684\u6309\u9215\u4e8b\u4ef6\u4f86\u5be6\u73fe\u756b\u9762\u90e8\u7f72\u53ca\u529f\u80fd\u3002\u672c\u7bc7\u6587\u7ae0\u5c07\u63d0\u4f9b **2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10**\uff0c\u8a73\u7d30\u4ecb\u7d39 UIButton \u7684\u4f7f\u7528\u3002<\/p>\n<h2>Button \u5ba3\u544a<\/h2>\n<p>UIButton \u662f\u7e7c\u627f\u81ea UIControl \u7684\u985e\u5225\uff0c\u800c UIControl \u53c8\u662f UIView \u7684\u5b50\u985e\u5225\uff0c\u56e0\u6b64 UIButton \u4e5f\u662f UIView \u7684\u4e00\u90e8\u5206\u3002<\/p>\n<p>&#8220;`swift<br \/>\nclass UIButton: UIControl {}<br \/>\nclass UIControl: UIView {}<br \/>\n&#8220;`<\/p>\n<h2>Button \u9ede\u64ca\u4e8b\u4ef6<\/h2>\n<p>\u7528\u6236\u9ede\u64ca\u6309\u9215\u6642\u6703\u900f\u904e Target-Action \u8a2d\u8a08\u6a21\u5f0f\u4f86\u901a\u77e5\u4f60\u7684\u7a0b\u5f0f\u3002\u9019\u7a2e\u65b9\u5f0f\u4e0d\u76f4\u63a5\u8655\u7406\u89f8\u6478\u4e8b\u4ef6\uff0c\u800c\u662f\u900f\u904e Interface Builder \u9023\u7d50\u5230\u4f60\u7684\u4e8b\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e09\u7a2e\u5e38\u898b\u7684 action \u65b9\u5f0f\uff1a<\/p>\n<h3>xib\/storyboard \u9023\u63a5\u4e8b\u4ef6<\/h3>\n<p>&#8220;`swift<br \/>\n@IBAction func doSomething()<br \/>\n@IBAction func doSomething(sender: UIButton)<br \/>\n@IBAction func doSomething(sender: UIButton, forEvent event: UIEvent)<br \/>\n&#8220;`<\/p>\n<h3>addTarget \u9023\u63a5\u4e8b\u4ef6<\/h3>\n<p>&#8220;`swift<br \/>\nbutton.addTarget(self, action: #selector(doSomething), for: .touchUpInside)<br \/>\n&#8220;`<\/p>\n<p>&#8220;`swift<br \/>\n@objc func doSomething()<br \/>\n@objc func doSomething(sender: UIButton)<br \/>\n@objc func doSomething(sender: UIButton, forEvent event: UIEvent)<br \/>\n&#8220;`<\/p>\n<h2>Button \u5e38\u7528\u624b\u52e2\u4ecb\u7d39<\/h2>\n<p>UIEvent \u4e2d\u5305\u542b\u5e7e\u7a2e\u624b\u52e2\u9ede\u64ca\u6a21\u5f0f\uff0c\u9019\u88e1\u662f\u5e7e\u500b\u5e38\u898b\u7684\u624b\u52e2\uff1a<\/p>\n<h3>touchDragEnter \u6309\u4f4f\u6ed1\u9032<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/imgur.com\/Ep2CgPu.gif\" alt=\"\u6309\u4f4f\u6ed1\u9032\u7684\u624b\u52e2\" title=\"\"><\/p>\n<h3>touchDragExit \u6309\u4f4f\u6ed1\u51fa<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/imgur.com\/Ep2CgPu.gif\" alt=\"\u6309\u4f4f\u6ed1\u51fa\u7684\u624b\u52e2\" title=\"\"><\/p>\n<h3>touchDown \u6309\u4e0b\u4e8b\u4ef6<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/imgur.com\/89xGR1V.gif\" alt=\"\u6309\u4e0b\u4e8b\u4ef6\" title=\"\"><\/p>\n<h3>touchUpInside \u6309\u4e0b\u653e\u958b<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/imgur.com\/89xGR1V.gif\" alt=\"\u653e\u958b\u4e8b\u4ef6\" title=\"\"><\/p>\n<h2>Button Layout \u756b\u9762\u8a2d\u8a08<\/h2>\n<p>UIButton \u7684\u7d44\u5408\u7531\u4ee5\u4e0b\u5e7e\u500b\u5143\u4ef6\u7d44\u6210\uff1a<br \/>\n&#8211; **BackgroundImage**: \u80cc\u666f\u5716\u7247<br \/>\n&#8211; **Image**: \u6309\u9215\u524d\u65b9\u5716\u7247<br \/>\n&#8211; **Title**: \u986f\u793a\u6587\u5b57<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/docs-assets.developer.apple.com\/published\/018b417326\/376d11ef-1d9b-4276-ad31-2a76628f7a29.png\" alt=\"UIButton Layout \u7d44\u6210\" title=\"\"><\/p>\n<h3>\u8a2d\u7f6e\u6309\u9215\u6587\u5b57<\/h3>\n<p>&#8220;`swift<br \/>\nfunc setTitle(_ title: String?, for state: UIControl.State)<br \/>\n&#8220;`<\/p>\n<h3>\u8a2d\u7f6e\u6309\u9215\u6587\u5b57\u984f\u8272<\/h3>\n<p>&#8220;`swift<br \/>\nfunc setTitleColor(_ color: UIColor?, for state: UIControl.State)<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u82e5 UIButton \u7684\u4e8b\u4ef6\u7121\u6cd5\u89f8\u767c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n1. \u78ba\u8a8d button \u662f\u5426\u5df2\u7d93\u6dfb\u52a0\u5230\u8996\u5716\u4e2d\u3002<br \/>\n2. \u78ba\u8a8d button \u7684 userInteractionEnabled \u5c6c\u6027\u70ba true\u3002<br \/>\n3. \u78ba\u8a8d action \u65b9\u6cd5\u662f\u5426\u6b63\u78ba\u9023\u63a5\u5230 button\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u4f7f\u7528\u65b9\u5f0f\uff0cUIButton \u9084\u53ef\u4ee5\u8207\u5176\u4ed6 UI \u5143\u4ef6\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u5229\u7528 Autolayout \u4f86\u5be6\u73fe\u97ff\u61c9\u5f0f\u8a2d\u8a08\uff0c\u6216\u8005\u8207\u52d5\u756b\u7d50\u5408\u4f86\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: UIButton \u548c UILabel \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: UIButton \u662f\u4e00\u500b\u53ef\u4e92\u52d5\u7684\u6309\u9215\u5143\u4ef6\uff0c\u5141\u8a31\u7528\u6236\u9ede\u64ca\uff0c\u800c UILabel \u53ea\u662f\u986f\u793a\u975c\u614b\u6587\u672c\uff0c\u4e0d\u5177\u4e92\u52d5\u6027\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u81ea\u5b9a\u7fa9 UIButton \u7684\u5916\u89c0\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u4f7f\u7528 setTitle\u3001setImage \u548c setBackgroundImage \u65b9\u6cd5\u4f86\u8a2d\u7f6e\u6309\u9215\u7684\u6587\u5b57\u3001\u5716\u7247\u548c\u80cc\u666f\u5716\u7247\uff0c\u4e26\u5229\u7528\u9670\u5f71\u548c\u908a\u6846\u4f86\u9032\u884c\u9032\u4e00\u6b65\u7684\u7f8e\u5316\u3002<\/p>\n<p>**Q3: UIButton \u652f\u63f4\u54ea\u4e9b\u624b\u52e2\uff1f**<br \/>\nA3: UIButton \u652f\u63f4\u591a\u7a2e\u624b\u52e2\uff0c\u5305\u62ec\u9ede\u64ca\u3001\u9577\u6309\u3001\u62d6\u52d5\u7b49\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u8981\u81ea\u5b9a\u7fa9\u624b\u52e2\u7684\u884c\u70ba\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c21\u4ecb \u6309\u9215\u662f iOS \u958b\u767c\u4e2d\u6700\u57fa\u790e\u7684 UI \u5143\u4ef6\u4e4b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","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":[5],"tags":[118,117],"class_list":["post-1139","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","tag-118","tag-117","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-in","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1139","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=1139"}],"version-history":[{"count":15,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1139\/revisions"}],"predecessor-version":[{"id":7788,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1139\/revisions\/7788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=1139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=1139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}