ios - 键盘在桌面 View 交互上消除了很多错误

标签 ios swift uitableview lag uikeyboard

我在 tableviewcontroller 中有一个文本字段,它位于 tabbarcontroller 内,当我向下拖动键盘时,它会在关闭之前滞后。我正在使用交互模式,因此键盘在拖动时会消失。

class TempTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.keyboardDismissMode = .interactive
        self.clearsSelectionOnViewWillAppear = false
        tableView.register(TitleTableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TitleTableViewCell
        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

    override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}

我的 tableviewcell 有一个 uitextfield。但是,当我向下拖动键盘时,它会在被关闭之前滞后

class TitleTableViewCell: UITableViewCell {


    let titleView: UITextField = {
       let textView = UITextField()
        textView.text = "Add your title ... "
        textView.font = UIFont.preferredFont(forTextStyle: .body)
//        textView.sizeToFit()
//        textView.isScrollEnabled = false
        textView.translatesAutoresizingMaskIntoConstraints=false
        return textView
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.contentView.addSubview(titleView)
        setTitleViewConstraints()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func setTitleViewConstraints(){
        titleView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor,constant: 10).isActive=true
        titleView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor,constant: -10).isActive=true
        titleView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 10).isActive=true
        titleView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor,constant: -10).isActive=true
    }

}

class tabBarViewController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        let homeViewController = HomeViewController()
        homeViewController.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "home-tab")?.withRenderingMode(.alwaysOriginal), selectedImage: nil)
        let discoverViewController = DiscoverViewController()
        discoverViewController.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "discover-tab")?.withRenderingMode(.alwaysOriginal), selectedImage: nil)
        let notificationViewController = NotificationViewController()
        notificationViewController.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "notification-tab")?.withRenderingMode(.alwaysOriginal), selectedImage: nil)
        let tempViewController = TempTableViewController()
        tempViewController.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "profile-tab")?.withRenderingMode(.alwaysOriginal), selectedImage: nil)


        let tabBarList = [homeViewController, discoverViewController,notificationViewController,tempViewController ]

            self.viewControllers = tabBarList.map { viewController in

            return UINavigationController(rootViewController: viewController)

        }
    }

}

这是 appdelegate 类,其中根据用户凭据选择 tabbarcontroller

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var handle: AuthStateDidChangeListenerHandle?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        UINavigationBar.appearance().isOpaque = false
        UINavigationBar.appearance().backgroundColor = .white
        window = UIWindow(frame: UIScreen.main.bounds)
        checkUserStatus()
        return true
    }

    func checkUserStatus(){
        window?.backgroundColor = UIColor.white
        self.setupRootViewController(viewController: UIViewController())
        handle = Auth.auth().addStateDidChangeListener {[weak self] auth,user in
            if(user != nil){
                self?.setupRootViewController(viewController: tabBarViewController())
            }else{
                self?.setupRootViewController(viewController: UINavigationController(rootViewController: WelcomeViewController()))
            }
        }
    }
    private func setupRootViewController(viewController: UIViewController) {
        self.window!.rootViewController = viewController
        self.window!.makeKeyAndVisible()
    }

enter image description here

最佳答案

我使用 iOS 12 启动了您的应用程序,它运行正常。

此解决方案可能会对您使用 iOS 13 有所帮助。

1) 在viewDidLoad()中添加观察者

override func viewDidLoad() {
    ...

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

2) 添加函数getKeyboardWindow()

private func getKeyboardWindow() -> UIWindow? {

    for window in UIApplication.shared.windows {

        if (NSStringFromClass(type(of: window).self) == "UIRemoteKeyboardWindow") {
            return window
        }
    }

    return nil
}

3) 添加函数keyboardWillHide(notification:)

@objc private func keyboardWillHide(notification: Notification) {

    guard let keyboardWindow: UIWindow = getKeyboardWindow() else { return }

    let screenWidth: CGFloat = UIScreen.main.bounds.width
    let screenHeight: CGFloat = UIScreen.main.bounds.height

    keyboardWindow.frame = CGRect(x: 0, y: 50, width: screenWidth, height: screenHeight)
}

附注但是,我不喜欢以这种方式解决问题。

但在这种情况下,我不知道更优雅的解决方案。

enter image description here

关于ios - 键盘在桌面 View 交互上消除了很多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59278526/

相关文章:

ios - 如何实现水平滚动/分页 View Controller ?

iphone - 设置委托(delegate)、数据源和文件所有者连接后,uitableView reloadData 不起作用

Xamarin 中的 iOS 构建大小

ios - 使用高阶 Swift 函数的潜在内存泄漏

swift - 设置状态栏颜色以从导航标题进行?

ios - 填充 TableView 中的 JSON 数组和 CoreData

ios - 如何在 Swift 的 tableView 单元格中添加一个开关?

ios - 无法验证您的应用程序错误

ios - 如果结果使用 NSLog 记录,如何创建单元测试

ios - 使用 .xib 文件在 Swift 中加载导航栏