键盘未出现在 UITextField 和 UITextView 中 IOS 15

Keyboard not appearing in UITextField and UITextView IOS 15

我没有为此应用程序使用故事板,所以这是我的 SceneDelegate willConnectTo 函数

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let windowScene = UIWindowScene(session: session, connectionOptions: connectionOptions)
    self.window = UIWindow(frame: UIScreen.main.bounds)
    
    self.window?.backgroundColor = .white
    let rootViewController = PersonsViewController()
    let navigationController = UINavigationController(rootViewController: rootViewController)
    navigationController.navigationBar.tintColor = .black
    
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
    self.window?.windowScene = windowScene
}

这里是UITextView配置函数

private func configureTextView() {
    textView = UITextView(frame: CGRect(x: 75, y: 120, width: view.frame.width, height: 120))
    textView.font = UIFont.systemFont(ofSize: 16)
    textView.textColor = .black
    textView.backgroundColor = .blue
    textView.delegate = self
    textView.becomeFirstResponder()
    textView.isUserInteractionEnabled = true
    
   
    textView.keyboardType = .default
    view.addSubview(textView)
}

这是在控制台中打印的内容

Could not find keyboard scene delegate for interaction view <_UITextLayoutCanvasView: 0x105904be0; frame = (0 0; 375 120); userInteractionEnabled = NO; layer = <CALayer: 0x282ed8ee0>>.

2022-02-07 08:33:15.481898+0200 Project[40078:7478637] [Assert] Could not find window to attach loupe view.

我确保调用了委托方法

以下代码有效:

  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let scene = (scene as? UIWindowScene) else { return }
    self.window = UIWindow(windowScene: scene)
    self.window?.backgroundColor = .white
    let rootViewController = ViewController()
    let navigationController = UINavigationController(rootViewController: rootViewController)
    navigationController.navigationBar.tintColor = .black
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
  }