{"id":46,"date":"2025-05-30T10:37:02","date_gmt":"2025-06-05T03:11:02","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=46"},"modified":"2025-06-05T10:37:02","modified_gmt":"2025-06-05T03:11:02","slug":"swift-%e6%8c%89%e9%88%95%e5%ae%a2%e8%a3%bd%e5%8c%96-%e5%9c%96%e7%89%87%e4%b8%8a%e6%96%b9-%e6%96%87%e5%ad%97%e4%b8%8b%e6%96%b9","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-%e6%8c%89%e9%88%95%e5%ae%a2%e8%a3%bd%e5%8c%96-%e5%9c%96%e7%89%87%e4%b8%8a%e6%96%b9-%e6%96%87%e5%ad%97%e4%b8%8b%e6%96%b9\/","title":{"rendered":"Swift \u6309\u9215\u5716\u7247\u8207\u6587\u5b57\u7684\u4e0a\u4e0b\u6392\u7248\u6559\u5b78\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>Swift \u6309\u9215\u5716\u7247\u8207\u6587\u5b57\u7684\u4e0a\u4e0b\u6392\u7248\u6559\u5b78<\/h1>\n<h2>\u7c21\u4ecb<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u9810\u8a2d\u7684 UIButton \u6703\u5c07\u5716\u7247\u8207\u6587\u5b57\u4e26\u6392\u986f\u793a\uff0c\u4f46\u6709\u6642\u6211\u5011\u5e0c\u671b\u80fd\u5920\u5c07\u5716\u7247\u653e\u5728\u4e0a\u9762\uff0c\u6587\u5b57\u653e\u5728\u4e0b\u9762\u6216\u76f8\u53cd\u3002\u70ba\u4e86\u9054\u6210\u9019\u500b\u6548\u679c\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u64f4\u5c55 UIButton \u4f86\u81ea\u8a02\u5176\u6392\u7248\u65b9\u5f0f\u3002\u672c\u6587\u5c07\u6559\u4f60\u5982\u4f55\u5728 Swift \u4e2d\u9054\u6210\u9019\u500b\u6548\u679c\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>\u5ba3\u544a UIButton<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b UIButton \u4e26\u8a2d\u5b9a\u5b83\u7684\u5716\u7247\u8207\u6587\u5b57\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5ba3\u544a UIButton \u4e26\u8abf\u7528\u6211\u5011\u81ea\u8a02\u7684\u6392\u7248\u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet lineBtn = UIButton(type: .system)<br \/>\nlineBtn.setImage(UIImage(named: &#8220;your_image_name&#8221;), for: .normal) \/\/ \u66ff\u63db\u70ba\u4f60\u7684\u5716\u7247\u540d\u7a31<br \/>\nlineBtn.setTitle(&#8220;\u6309\u9215\u6587\u5b57&#8221;, for: .normal)<br \/>\nlineBtn.centerImageAndButton(10, imageOnTop: true) \/\/ \u8a2d\u5b9a\u5716\u7247\u5728\u4e0a\uff0c\u6587\u5b57\u5728\u4e0b<br \/>\n&#8220;`<\/p>\n<h2>\u64f4\u5145 UIButton<\/h2>\n<p>\u70ba\u4e86\u5be6\u73fe\u5716\u7247\u8207\u6587\u5b57\u7684\u4e0a\u4e0b\u6392\u7248\uff0c\u6211\u5011\u9700\u8981\u64f4\u5c55 UIButton\uff0c\u4e26\u65b0\u589e\u4e00\u500b\u540d\u70ba `centerImageAndButton` \u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nextension UIButton {<br \/>\n    func centerImageAndButton(_ gap: CGFloat, imageOnTop: Bool) {<br \/>\n        guard let imageView = self.currentImage,<br \/>\n              let titleLabel = self.titleLabel?.text else { return }<\/p>\n<p>        let sign: CGFloat = imageOnTop ? 1 : -1<br \/>\n        self.titleEdgeInsets = UIEdgeInsets(top: (imageView.size.height + gap) * sign,<br \/>\n                                             left: -imageView.size.width,<br \/>\n                                             bottom: 0,<br \/>\n                                             right: 0)<\/p>\n<p>        let titleSize = titleLabel.size(withAttributes: [NSAttributedString.Key.font: self.titleLabel!.font!])<br \/>\n        self.imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + gap) * sign,<br \/>\n                                             left: 0,<br \/>\n                                             bottom: 0,<br \/>\n                                             right: -titleSize.width)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e7e\u500b\u5e38\u898b\u554f\u984c\uff1a<br \/>\n1. **\u6309\u9215\u7121\u6cd5\u986f\u793a\u5716\u7247\u6216\u6587\u5b57**\uff1a\u6aa2\u67e5\u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u4e86\u5716\u7247\u548c\u6587\u5b57\uff0c\u4e26\u78ba\u4fdd\u5716\u7247\u540d\u7a31\u7121\u8aa4\u3002<br \/>\n2. **\u6392\u7248\u6548\u679c\u4e0d\u5982\u9810\u671f**\uff1a\u78ba\u8a8d `gap` \u53c3\u6578\u662f\u5426\u7b26\u5408\u4f60\u7684\u9700\u6c42\uff0c\u9069\u7576\u8abf\u6574\u9593\u8ddd\u53ef\u4ee5\u9054\u5230\u66f4\u597d\u7684\u6392\u7248\u6548\u679c\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u6211\u5011\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55 UIButton \u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; \u6dfb\u52a0\u4e0d\u540c\u72c0\u614b\u4e0b\u7684\u5716\u7247\u8207\u6587\u5b57\u6392\u7248\u3002<br \/>\n&#8211; \u6839\u64da UI \u8a2d\u8a08\u9700\u6c42\u52d5\u614b\u8abf\u6574\u5716\u7247\u8207\u6587\u5b57\u7684\u4f4d\u7f6e\u3002<\/p>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><br \/>\n    <img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"\u6309\u9215\u5716\u7247\u5728\u4e0a\" title=\"\"><\/p>\n<p>\u9019\u662f\u4e00\u500b\u793a\u7bc4\u6309\u9215\uff0c\u5716\u7247\u5728\u4e0a\uff0c\u6587\u5b57\u5728\u4e0b\u7684\u6548\u679c\u3002<\/p>\n<p><\/a><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>\u5982\u4f55\u8b93\u5716\u7247\u5728\u4e0b\uff0c\u6587\u5b57\u5728\u4e0a\uff1f<\/h3>\n<p>\u53ea\u9700\u5c07 `imageOnTop` \u53c3\u6578\u8a2d\u70ba `false` \u5373\u53ef\u3002<\/p>\n<h3>\u6211\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u7fa9\u5716\u7247\u5c3a\u5bf8\u55ce\uff1f<\/h3>\n<p>\u53ef\u4ee5\uff0c\u8acb\u5728\u8a2d\u7f6e\u5716\u7247\u4e4b\u524d\uff0c\u5148\u5c07\u5716\u7247\u8abf\u6574\u70ba\u6240\u9700\u7684\u5c3a\u5bf8\u3002<\/p>\n<h3>\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u7528\u65bc\u5176\u4ed6 UI \u5143\u7d20\u55ce\uff1f<\/h3>\n<p>\u6b64\u65b9\u6cd5\u5c08\u70ba UIButton \u8a2d\u8a08\uff0c\u4f46\u4f60\u53ef\u4ee5\u6839\u64da\u9700\u8981\u9032\u884c\u8abf\u6574\u4ee5\u61c9\u7528\u65bc\u5176\u4ed6 UI \u5143\u7d20\u3002<br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`html Swift \u6309\u9215\u5716\u7247\u8207\u6587&#8230;<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift"],"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-K","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/46","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=46"}],"version-history":[{"count":7,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":1132,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/46\/revisions\/1132"}],"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=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}