{"id":1343,"date":"2020-12-20T22:35:42","date_gmt":"2020-12-20T14:35:42","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1343"},"modified":"2021-09-15T16:08:19","modified_gmt":"2021-09-15T08:08:19","slug":"swiftui-ios14-%e9%94%ae%e7%9b%98%e8%87%aa%e5%8a%a8%e4%b8%8a%e7%a7%bb%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1343","title":{"rendered":"swiftUI ios14 \u952e\u76d8\u81ea\u52a8\u4e0a\u79fb\u7684\u95ee\u9898"},"content":{"rendered":"\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\/2020\/12\/Ql055.gif\" alt=\"\" class=\"wp-image-1344\" width=\"244\" height=\"505\"\/><\/figure><\/div>\n\n\n\n<p>SwiftUI\u66f4\u65b0\u5230ios14\u4ee5\u540e\uff0c\u6211\u4eec\u4f7f\u7528Textfiled\u65f6\uff0c\u4f1a\u53d1\u73b0\u952e\u76d8\u5f39\u51fa\u65f6\uff0c\u4f1a\u5bfc\u81f4view\u4e0a\u79fb\u3002\u4e0d\u77e5\u9053\u662f\u4e0d\u662fios14\u7684bug\uff1f<\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6cd5\u6709\u4e24\u79cd\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=\"\">struct ContentView: View {\n    @State var text: String = \"\"\n    var body: some View {\n        VStack {\n            \/\/ \u5fc5\u987b\u8981\u52a0Spacer \u8981\u4e0d\u4e0d\u597d\u7528\n            Spacer()\n            TextField(\"asd\", text: self.$text)\n                .textFieldStyle(RoundedBorderTextFieldStyle())\n            Spacer()\n        }\n        .ignoresSafeArea(.keyboard, edges: .bottom)\n    }\n}<\/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=\"\">\u5229\u7528GeometryReader\nGeometryReader { _ in\n    ZStack {\n        \/\/PUT CONTENT HERE\n    }.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)\n}\n<\/pre>\n\n\n\n<ul><li>\u76ee\u524d\u5c1d\u8bd5\u6700\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u5728\u6700\u6839\u7684MainView\u4f7f\u7528GeometryReader\u5305\u4f4fZStack\u3002<\/li><\/ul>\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 MainView: View {\n  \n    var body: some View {\n        GeometryReader { _ in\n            ZStack{\n                 if A {\n                   Aview()\n                 } else if B {\n                   Bview()\n                 }\n \n            }.edgesIgnoringSafeArea(.all)}\n    }\n    \n}<\/pre>\n\n\n\n<ul><li>\u8fd8\u6709\u7528.ignoresSafeArea(.keyboard, edges: .bottom)\u7684\u5199\u6cd5<\/li><\/ul>\n\n\n\n<p><\/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=\"\">\u4f8b1\nstruct ContentView: View {\n    @State var text: String = \"\"\n\n    var body: some View {\n        if #available(iOS 14.0, *) {\n            VStack {\n                content\n            }\n            .ignoresSafeArea(.keyboard, edges: .bottom)\n        } else {\n            VStack {\n                content\n            }\n        }\n    }\n\n    @ViewBuilder\n    var content: some View {\n        Spacer()\n        TextField(\"asd\", text: self.$text)\n            .textFieldStyle(RoundedBorderTextFieldStyle())\n        Spacer()\n    }\n}\n\n\u4f8b2\nstruct ContentView: View {\n    @State var text: String = \"\"\n    var body: some View {\n        GeometryReader { _ in\n            ...\n        }\n        .ignoresSafeArea(.keyboard, edges: .bottom)\n    }\n}\n\n\u4f8b3\nstruct ContentView: View {\n    @State var text: String = \"\"\n    var body: some View {\n        VStack {\n            Spacer()\n            TextField(\"asd\", text: self.$text)\n                .textFieldStyle(RoundedBorderTextFieldStyle())\n            Spacer()\n        }\n        .ignoresSafeArea(.keyboard, edges: .bottom)\n    }\n}\n<\/pre>\n\n\n\n<p>\u952e\u76d8\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u906e\u6321\u5165\u529b\u6846\uff0c\u6211\u4eec\u53ef\u4ee5\u6d3b\u7528inputAccessoryView<\/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\/2021\/03\/image-47.png\" alt=\"\" class=\"wp-image-2114\" width=\"164\" height=\"87\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"456\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/03\/image-49-1024x456.png\" alt=\"\" class=\"wp-image-2116\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-1024x456.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-300x134.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-768x342.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-1536x684.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-2048x912.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-830x370.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-230x102.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-350x156.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/03\/image-49-480x214.png 480w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b<\/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=\"\">struct NumberTextfield: UIViewRepresentable {\n    \n    @Binding var text: String\n    @State var textbar: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))\n    \n    func makeUIView(context: Context) -> UITextField {\n        let view = UITextField()\n        view.setLeftPaddingPoints(10)\n        view.isUserInteractionEnabled = true\n        view.backgroundColor = uiColorF9F9F9\n        view.layer.borderColor = uiColorF9F9F9.cgColor\n        view.layer.borderWidth = 1\n        view.layer.cornerRadius = 4\n        view.textColor = uiColor666666\n        view.delegate = context.coordinator\n        view.keyboardType = UIKeyboardType.numberPad\n        view.clearButtonMode = .whileEditing\n        view.frame = CGRect(x: 0, y: 0, width: 30, height: 40)\n       \n        textbar.font = UIFont.systemFont(ofSize: 23)\n        textbar.backgroundColor = UIColor.init(red: 245 \/ 255, green: 245 \/ 255, blue: 245 \/ 255, alpha: 1)\n        textbar.isEnabled = false\n        view.inputAccessoryView = textbar\n        \n        return view\n    }\n    \n    func updateUIView(_ uiView: UITextField, context: Context) {\n        uiView.text = text\n        textbar.text = uiView.text\n    }\n    \n    func makeCoordinator() -> NumberTextfield.Coordinator {\n        Coordinator(self)\n    }\n    \n    class Coordinator: NSObject, UITextFieldDelegate {\n        var control: NumberTextfield\n        \n        init(_ control: NumberTextfield) {\n            self.control = control\n        }\n        \n        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {\n            self.control.text = textField.text!\n            return true\n        }\n        \n        func textFieldDidChangeSelection(_ textField: UITextField) {\n            if((textField.text?.count)! > 10) {\n                textField.text! = String(textField.text!.prefix(10))\n            }\n            self.control.text = textField.text!\n        }\n    \n    }\n    \n}<\/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 TextAreaView: UIViewRepresentable {\n    \n    @Binding var text: String\n    @State var textbar: UITextView = UITextView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))\n    \n    func makeUIView(context: Context) -> UITextView {\n        let view = UITextView()\n        view.isScrollEnabled = true\n        view.isEditable = true\n        view.font = UIFont.systemFont(ofSize: 15)\n        view.isUserInteractionEnabled = true\n        view.layer.cornerRadius = 4\n        view.backgroundColor = uiColorF9F9F9\n        view.delegate = context.coordinator\n        view.keyboardType = .default\n        \n        textbar.isScrollEnabled = true\n        textbar.isEditable = false\n        textbar.isUserInteractionEnabled = false\n        textbar.textContainerInset = UIEdgeInsets(top: 8, left: 4, bottom: 8, right: 4)\n        textbar.font = UIFont.systemFont(ofSize: 19)\n        textbar.backgroundColor = UIColor.init(red: 245 \/ 255, green: 245 \/ 255, blue: 245 \/ 255, alpha: 1)\n        view.inputAccessoryView = textbar\n        \n        return view\n    }\n    \n    func updateUIView(_ uiView: UITextView, context: Context) {\n        textbar.text = uiView.text\n    }\n    \n    func makeCoordinator() -> TextAreaView.Coordinator {\n        Coordinator(self)\n    }\n    \n    class Coordinator: NSObject, UITextViewDelegate {\n        var control: TextAreaView\n        \n        init(_ control: TextAreaView) {\n            self.control = control\n        }\n      \n        func textViewDidChange(_ textView: UITextView) {\n            self.control.text = textView.text!\n            self.control.textbar.text = textView.text!\n            \/\/let range = NSMakeRange(textView.text.count - 1, 0)\n            self.control.textbar.scrollRangeToVisible(textView.selectedRange)\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SwiftUI\u66f4\u65b0\u5230ios14\u4ee5\u540e\uff0c\u6211\u4eec\u4f7f\u7528Textfiled\u65f6\uff0c\u4f1a\u53d1\u73b0\u952e\u76d8\u5f39\u51fa\u65f6\uff0c\u4f1a\u5bfc\u81f4view\u4e0a\u79fb\u3002\u4e0d\u77e5\u9053 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1343"}],"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=1343"}],"version-history":[{"count":9,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1343\/revisions"}],"predecessor-version":[{"id":3885,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1343\/revisions\/3885"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}