簡體   English   中英

為什么 SwiftUI 中的 TextEditor 會忽略我的 .keyboardType?

[英]Why does my TextEditor in SwiftUI ignore my .keyboardType?

我正在嘗試在 SwiftUI 中使用TextEditor()並且需要.keyboardType(.numberPad) 不幸的是,每當我設置這個選項時,出於某種原因,我的設備和模擬器似乎完全忽略了它! 無論我將什么設置為鍵盤選項,它都只顯示默認鍵盤。

有誰知道為什么會這樣? 我已經使用 Xcode 12 Beta 2 測試了 iOS 14.0 和 14.2 Beta。

這是使用SwiftUI-Introspect的丑陋解決方法

(FB8816771 是我用 Apple 記錄的反饋 ID。)

private extension View {
    func keyboardType_FB8816771(_ type: UIKeyboardType) -> some View {
        let customise: (UITextView) -> () = { uiTextView in
            uiTextView.keyboardType = type
        }
        
        return introspect(selector: TargetViewSelector.siblingContaining, customize: customise)
    }
}

如果這可以幫助任何人解決 TextEditor 沒有“完成”按鈕的事實……擴展上面盧克霍華德的答案……

import Introspect

extension View {
    func addDoneButton() -> some View {
        let helper = MainViewHelper()
        
        let customise: (UITextView) -> () = { uiTextView in
            let toolBar = UIToolbar(frame: CGRect(x: 0.0,
                                                  y: 0.0,
                                                  width: UIScreen.main.bounds.size.width,
                                                  height: 44.0))//1
            let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
            let barButton = UIBarButtonItem(title: "Done", style: .plain, target: uiTextView, action: #selector(helper.close))
            toolBar.setItems([flexible, barButton], animated: false)//4
            uiTextView.inputAccessoryView = toolBar
            uiTextView.keyboardAppearance = .light
        }
        
        return introspect(selector: TargetViewSelector.siblingContaining, customize: customise)
    }
}

class MainViewHelper {
    @objc func close() {
        
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM