{"id":7405,"date":"2022-09-18T09:07:23","date_gmt":"2022-09-18T01:07:23","guid":{"rendered":"http:\/\/123.57.164.21\/?p=7405"},"modified":"2022-09-18T12:56:02","modified_gmt":"2022-09-18T04:56:02","slug":"swift-uiscrollview%e4%b8%ad%e7%9a%84keyboard%e9%80%82%e9%85%8d-%e5%a6%82%e4%bd%95%e5%9c%a8%e5%bc%b9%e5%87%ba%e8%bd%af%e9%94%ae%e7%9b%98%e6%97%b6%e6%8a%8aview%e5%90%91%e4%b8%8a%e7%a7%bb%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=7405","title":{"rendered":"Swift-UIScrollView\u4e2d\u7684keyboard\u9002\u914d-\u5982\u4f55\u5728\u5f39\u51fa\u8f6f\u952e\u76d8\u65f6\u628aview\u5411\u4e0a\u79fb\u52a8\u952e\u76d8\u7684\u9ad8\u5ea6"},"content":{"rendered":"\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f39\u51fa\u8f6f\u952e\u76d8\u65f6\uff0cUIScrollView\uff08\u5305\u542b\u4ed6\u7684\u5b50\u7c7bUITableView\uff0cUITextView\uff0cUICollectionView\u7b49\uff09\u662f\u4e0d\u4f1a\u81ea\u52a8\u4e0a\u79fb\u7684\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u5c4f\u5e55\u91cc\u9760\u4e0b\u7684\u5185\u5bb9\u4f1a\u88ab\u8f6f\u952e\u76d8\u6321\u4f4f\uff0c\u7528\u6237\u65e0\u6cd5\u5b9e\u65f6\u770b\u5230\u81ea\u5df1\u5f53\u524d\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u8ba9UIScrollView\u5728\u8f6f\u952e\u76d8\u5f39\u51fa\u7684\u65f6\u5019\u5411\u4e0a\u79fb\u52a8\u952e\u76d8\u7684\u9ad8\u5ea6<\/p>\n\n\n\n<p>1.\u4ee5UIScrollView\u4e3a\u4f8b\uff0c\u6ce8\u518c\u4e24\u4e2a\u89c2\u5bdf\u8005\uff0c\u5206\u522b\u76d1\u6d4b\u952e\u76d8\u6d88\u5931\u548c\u952e\u76d8\u51fa\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@IBOutlet weak var textView: UITextView!\n\noverride func viewDidLoad() {\n    super.viewDidLoad()\n\n    NotificationCenter.default.addObserver(self, selector: #selector(adjustKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)\n    NotificationCenter.default.addObserver(self, selector: #selector(adjustKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)\n    \n}\n<\/pre>\n\n\n\n<p>2.\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@objc func adjustKeyboard(notification: Notification) {\n        \n        guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }\n\n        let keyboardScreenEndFrame = keyboardValue.cgRectValue\n        let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)\n\n        if notification.name == UIResponder.keyboardWillHideNotification {\n            self.scrollView.contentInset = .zero\n        } else {\n            \/\/ \u5176\u5b9e\u5c31\u662f\u8c03\u6574\u5185\u8fb9\u8ddd\u7684bottom\n            self.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height - view.safeAreaInsets.bottom, right: 0)\n        }\n\n        self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset \/\/ \u4fdd\u8bc1\u6eda\u52a8\u6761\u548c\u5185\u5bb9\u76f8\u5bf9\u5e94\n\n        let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.height + scrollView.contentInset.bottom)\n        \n        \/\/ \u6eda\u52a8\u5230\u5e95\u90e8\n        scrollView.setContentOffset(bottomOffset, animated: true)\n    }\n<\/pre>\n\n\n\n<p>\u6ce8\uff1a<\/p>\n\n\n\n<p>\u5927\u591a\u6570\u60c5\u51b5\uff0c\u6211\u4eec\u7684UITextView\u90fd\u662f\u7ea6\u675f\u5728safearea\u91cc\u9762\u7684\uff0c\u6240\u4ee5\u5728\u4e0a\u8ff0\u8c03\u6574\u5185\u8fb9\u8dddbottom\u7684\u6838\u5fc3\u4ee3\u7801\u4e2d\uff0c\u9700\u51cf\u53bb<code>view.safeAreaInsets.bottom<\/code>\uff0c\u4e0d\u7136\u8f6f\u952e\u76d8\u548cUITextView\u5185\u5bb9\u4e4b\u95f4\u4f1a\u6709\u4e2a\u7a7a\u6863\uff08\u5373bottom\u5185\u8fb9\u8ddd\u592a\u5927\uff0c\u8981\u51cf\u53bb\u4e00\u90e8\u5206\uff09\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-75-479x1024.png\" alt=\"\" class=\"wp-image-7406\" width=\"252\" height=\"539\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-75-479x1024.png 479w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-75-140x300.png 140w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-75-230x491.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-75-350x748.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-75-480x1025.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-75.png 560w\" sizes=\"(max-width: 252px) 100vw, 252px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f39\u51fa\u8f6f\u952e\u76d8\u65f6\uff0cUIScrollView\uff08\u5305\u542b\u4ed6\u7684\u5b50\u7c7bUITableView\uff0cUITextView\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7405"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7405"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7405\/revisions"}],"predecessor-version":[{"id":7409,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7405\/revisions\/7409"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}