{"id":7058,"date":"2025-06-01T15:03:08","date_gmt":"2025-06-05T07:27:08","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7058"},"modified":"2025-06-05T15:03:08","modified_gmt":"2025-06-05T07:27:08","slug":"https-badgameshow-com-steven-204","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-204\/","title":{"rendered":"Swift \u9375\u76e4\u81ea\u52d5\u4e0a\u79fb\u65b9\u6cd5\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u9375\u76e4\u81ea\u52d5\u4e0a\u79fb, iOS \u958b\u767c, Swift \u8a9e\u6cd5\"><\/p>\n<h2>Swift \u9375\u76e4\u81ea\u52d5\u4e0a\u79fb\u65b9\u6cd5\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u7528\u65bc\u958b\u767c iOS \u548c macOS \u61c9\u7528\u7a0b\u5f0f\u7684\u7c21\u55ae\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u80fd\u5920\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u5728\u958b\u767c iOS \u61c9\u7528\u7a0b\u5f0f\u6642\uff0c\u6211\u5011\u7d93\u5e38\u6703\u9047\u5230\u9375\u76e4\u906e\u64cb\u6587\u5b57\u8f38\u5165\u6846\u7684\u554f\u984c\u3002\u9019\u6642\u5019\uff0c\u4f7f\u7528 Swift \u7684\u81ea\u52d5\u9375\u76e4\u4e0a\u79fb\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h3>\u76e3\u807d\u9375\u76e4\u901a\u77e5<\/h3>\n<p>\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>NotificationCenter<\/strong> \u4f86\u8a3b\u518a\u9375\u76e4\u51fa\u73fe\u548c\u6d88\u5931\u7684\u901a\u77e5\u3002\u4e0b\u9762\u662f\u5982\u4f55\u5be6\u4f5c\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)\nNotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)\n<\/code><\/pre>\n<h3>\u5be6\u4f5c\u9375\u76e4\u4e0a\u79fb\u8207\u4e0b\u79fb\u65b9\u6cd5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5be6\u4f5c <strong>keyboardWillShow<\/strong> \u548c <strong>keyboardWillHide<\/strong> \u5169\u500b\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5728\u9375\u76e4\u51fa\u73fe\u548c\u6d88\u5931\u6642\uff0c\u81ea\u52d5\u5c07\u6587\u5b57\u8f38\u5165\u6846\u4e0a\u79fb\u6216\u4e0b\u79fb\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func keyboardWillShow(notification: NSNotification) {\n    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {\n        if self.view.frame.origin.y == 0 {\n            self.view.frame.origin.y -= keyboardSize.height\n        }\n    }\n}\n\n@objc func keyboardWillHide(notification: NSNotification) {\n    if self.view.frame.origin.y != 0 {\n        self.view.frame.origin.y = 0\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <strong>keyboardWillShow<\/strong> \u548c <strong>keyboardWillHide<\/strong> \u5169\u500b\u65b9\u6cd5\u4f86\u63a7\u5236\u756b\u9762\u7684\u79fb\u52d5\uff0c\u78ba\u4fdd\u6587\u5b57\u8f38\u5165\u6846\u4e0d\u88ab\u9375\u76e4\u906e\u64cb\u3002<\/p>\n<h3>\u8655\u7406\u9375\u76e4\u986f\u793a\u8207\u96b1\u85cf\u7684\u52d5\u756b<\/h3>\n<p>\u70ba\u4e86\u8b93\u9375\u76e4\u51fa\u73fe\u548c\u6d88\u5931\u7684\u904e\u7a0b\u66f4\u5e73\u6ed1\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u52d5\u756b\u4e2d\u8655\u7406\u9019\u4e9b\u8b8a\u5316\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func keyboardWillShow(notification: NSNotification) {\n    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {\n        UIView.animate(withDuration: 0.3) {\n            self.view.frame.origin.y -= keyboardSize.height\n        }\n    }\n}\n\n@objc func keyboardWillHide(notification: NSNotification) {\n    UIView.animate(withDuration: 0.3) {\n        self.view.frame.origin.y = 0\n    }\n}\n<\/code><\/pre>\n<h3>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h3>\n<p>\u78ba\u4fdd\u5728\u9069\u7576\u7684\u5730\u65b9\u8a3b\u518a\u548c\u89e3\u9664\u901a\u77e5\uff0c\u901a\u5e38\u662f\u5728 <strong>viewWillAppear<\/strong> \u548c <strong>viewWillDisappear<\/strong> \u4e2d\u9032\u884c\u8a3b\u518a\u548c\u89e3\u9664\u8a3b\u518a\uff0c\u4ee5\u907f\u514d\u6f5b\u5728\u7684\u8a18\u61b6\u9ad4\u6d29\u6f0f\u6216\u61c9\u7528\u5d29\u6f70\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">override func viewWillAppear(_ animated: Bool) {\n    super.viewWillAppear(animated)\n    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)\n    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)\n}\n\noverride func viewWillDisappear(_ animated: Bool) {\n    super.viewWillDisappear(animated)\n    NotificationCenter.default.removeObserver(self)\n}\n<\/code><\/pre>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u9664\u4e86\u9375\u76e4\u7684\u4e0a\u79fb\u8207\u4e0b\u79fb\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u8003\u616e\u5728\u9375\u76e4\u51fa\u73fe\u6642\u81ea\u52d5\u6372\u52d5 <strong>UIScrollView<\/strong>\uff0c\u4ee5\u78ba\u4fdd\u8f38\u5165\u6846\u59cb\u7d42\u53ef\u898b\uff0c\u9019\u5c0d\u65bc\u8868\u55ae\u8f38\u5165\u4f86\u8aaa\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u81ea\u52d5\u9375\u76e4\u4e0a\u79fb\u65b9\u6cd5-\ud83d\udcbb_1675401601.605696.webp\" alt=\"Swift \u81ea\u52d5\u9375\u76e4\u4e0a\u79fb\u65b9\u6cd5 \ud83d\udcbb\" title=\"Swift \u81ea\u52d5\u9375\u76e4\u4e0a\u79fb\u3001Swift \u9375\u76e4\u4e0a\u79fb\u65b9\u6cd5\u3001Swift \u81ea\u52d5\u4e0a\u79fb\u9375\u76e4\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u78ba\u4fdd\u9375\u76e4\u4e0d\u906e\u64cb\u6240\u6709\u7684\u6587\u5b57\u8f38\u5165\u6846\uff1f<\/h3>\n<p>A1: \u5728\u5be6\u4f5c\u9375\u76e4\u901a\u77e5\u6642\uff0c\u53ef\u4ee5\u5c07\u6240\u6709\u8f38\u5165\u6846\u7684\u5750\u6a19\u8a08\u7b97\u9032\u53bb\uff0c\u4e26\u6839\u64da\u9375\u76e4\u7684\u9ad8\u5ea6\u8abf\u6574\u6574\u500b\u8996\u5716\u7684\u79fb\u52d5\u91cf\u3002<\/p>\n<h3>Q2: \u4f7f\u7528 UIScrollView \u6642\u5982\u4f55\u8655\u7406\u9375\u76e4\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\u5728\u9375\u76e4\u986f\u793a\u6642\u4f7f\u7528 <strong>UIScrollView<\/strong> \u7684 <strong>scrollRectToVisible<\/strong> \u65b9\u6cd5\uff0c\u78ba\u4fdd\u7576\u524d\u8f38\u5165\u6846\u5728\u53ef\u898b\u7bc4\u570d\u5167\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u89e3\u9664\u9375\u76e4\u7684\u81ea\u52d5\u4e0a\u79fb\uff1f<\/h3>\n<p>A3: \u82e5\u8981\u505c\u6b62\u9375\u76e4\u81ea\u52d5\u4e0a\u79fb\uff0c\u53ea\u9700\u79fb\u9664\u76e3\u807d\u5668\u6216\u5728\u9700\u8981\u6642\u4e0d\u8abf\u7528\u4e0a\u79fb\u7684\u7a0b\u5f0f\u78bc\u5373\u53ef\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u8a9e\u8a00\u4e2d\u7684\u81ea\u52d5\u9375\u76e4\u4e0a\u79fb\u65b9\u6cd5\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5b78\u7fd2\u5982\u4f55\u5728iOS\u548cmacOS\u4e0a\u81ea\u52d5\u4e0a\u79fb\u9375\u76e4\uff0c\u8b93\u4f60\u7684App\u66f4\u52a0\u5b8c\u7f8e\u3002<\/p>\n","protected":false},"author":1,"featured_media":7057,"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-7058","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-\u81ea\u52d5\u9375\u76e4\u4e0a\u79fb\u65b9\u6cd5-\ud83d\udcbb_1675401601.605696.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1PQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7058","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=7058"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7058\/revisions"}],"predecessor-version":[{"id":13243,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7058\/revisions\/13243"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7057"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}