{"id":110,"date":"2020-07-22T16:25:07","date_gmt":"2020-07-22T08:25:07","guid":{"rendered":"http:\/\/123.57.164.21\/?p=110"},"modified":"2020-08-05T12:19:05","modified_gmt":"2020-08-05T04:19:05","slug":"swiftui%e5%8f%96%e5%be%97%e9%94%ae%e7%9b%98%e9%ab%98%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=110","title":{"rendered":"SwiftUI\u53d6\u5f97\u952e\u76d8\u9ad8\u5ea6"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"alignright size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/07\/image-4.png\" alt=\"\" class=\"wp-image-111\" width=\"149\" height=\"87\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-4.png 690w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-4-300x175.png 300w\" sizes=\"(max-width: 149px) 100vw, 149px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u5728App\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5f80\u5f80\u9700\u8981\u6839\u636e\u5f39\u51fa\u952e\u76d8\u7684\u9ad8\u5ea6\u6765\u54cd\u5e94\u6211\u4eec\u7684\u5e03\u5c40\uff0c\u6240\u4ee5\u80fd\u5426\u5b9e\u65f6\u53d6\u5f97\u6b63\u786e\u7684\u952e\u76d8\u9ad8\u5ea6\u6570\u503c\u662f\u4e00\u4e2a\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\uff0c\u6765\u5b9e\u65f6\u83b7\u53d6\u5f39\u51fa\u952e\u76d8\u7684\u9ad8\u5ea6\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">Step 1: Create our keyboard height helper class<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u521b\u5efa KeyboardHeightHelper\u7c7b\uff0c\u5e76\u4e14\u5b9e\u73b0ObservableObject\u63a5\u53e3\nclass KeyboardHeightHelper: ObservableObject {\n    \n    \/\/ \u952e\u76d8\u9ad8\u5ea6\n    @Published var keyboardHeight: CGFloat = 0\n\n    private func listenForKeyboardNotifications() {\n        \n        \/\/ \u76d1\u542c\u952e\u76d8\u5f39\u51fa\u4e8b\u4ef6\n        NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification,\n            object: nil,\n            queue: .main) { (notification) in\n            guard let userInfo = notification.userInfo,\n                let keyboardRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }\n                print(keyboardRect.height)\n            self.keyboardHeight = keyboardRect.height\n            \/\/ \u901a\u8fc7NotificationCenter\u628a\u952e\u76d8\u5f39\u51fa\u7684\u4e8b\u4ef6\u4f20\u9012\u51fa\u53bb\uff0cSwiftUI\u53ef\u4ee5\u901a\u8fc7onReceive\u76d1\u542c\u63a5\u6536\u5230\u901a\u77e5\u3002\n            NotificationCenter.default.post(name: .editBegin, object: nil, userInfo: nil)\n        }\n\n        \/\/ \u76d1\u542c\u952e\u76d8\u5173\u95ed\u4e8b\u4ef6\n        NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification,\n            object: nil,\n            queue: .main) { (notification) in\n            self.keyboardHeight = 0\n            \/\/ \u901a\u8fc7NotificationCenter\u628a\u952e\u76d8\u5f39\u51fa\u7684\u4e8b\u4ef6\u4f20\u9012\u51fa\u53bb\uff0cSwiftUI\u53ef\u4ee5\u901a\u8fc7onReceive\u76d1\u542c\u63a5\u6536\u5230\u901a\u77e5\u3002\n            NotificationCenter.default.post(name: .editEnd, object: nil, userInfo: nil)\n        }\n    }\n    \n    init() {\n        self.listenForKeyboardNotifications()\n    }\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">Step 2: Use the keyboardHeight value<\/pre>\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=\"\">struct keyBoardDemoView{\n\/\/ \u5b9a\u4e49keyboardHeightHelper\u53d8\u91cf\uff0c\u8bbe\u5b9a\u4e3aObservedObject\u7c7b\u578b\u3002\n@ObservedObject var keyboardHeightHelper = KeyboardHeightHelper()\n@State var textFieldText = \"\"\n\nvar body: some View {\n    VStack {\n        Spacer()\n        TextField(\"Text field\",\n                  text: $textFieldText)\n             \/\/ \u53ef\u4ee5\u5229\u7528\u76d1\u542c\u5f97\u5230\u7684keyboardHeight\u952e\u76d8\u9ad8\u5ea6\uff0c\u901a\u8fc7SwiftUI\u7684offset\u81ea\u7531\u7684\u8c03\u6574\u6587\u672c\u6846\u7684\u4f4d\u7f6e\uff0c\u4ee5\u514d\u88ab\u5f39\u51fa\u952e\u76d8\u8986\u76d6\u6389\u3002\n            .offset(y: -self.keyboardHeightHelper.keyboardHeight)\n    }\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\"><div class=\"wp-block-group__inner-container\"><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728App\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5f80\u5f80\u9700\u8981\u6839\u636e\u5f39\u51fa\u952e\u76d8\u7684\u9ad8\u5ea6\u6765\u54cd\u5e94\u6211\u4eec\u7684\u5e03\u5c40\uff0c\u6240\u4ee5\u80fd\u5426\u5b9e\u65f6\u53d6\u5f97\u6b63\u786e\u7684\u952e\u76d8\u9ad8\u5ea6\u6570\u503c\u662f\u4e00\u4e2a\u7ecf\u5e38\u4f1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/110"}],"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=110"}],"version-history":[{"count":21,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":185,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions\/185"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}