键盘消除了TableView交互式上的非常多的错误

时间:2019-12-11 03:30:41

标签: 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

4 个答案:

答案 0 :(得分:5)

我使用 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)
}

P.S。但是,我不喜欢这样解决问题。

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

enter image description here

答案 1 :(得分:1)

doe

您所做的是正确的,我认为这不是问题,即使通讯录应用也具有与您相同的功能(即表格视图中的文本字段)。联系人应用程序的行为也相同

找到联系人应用与您的比较屏幕录像,

Contacts AppYour App

如果您仍要修复它,建议您在需要时删除自动更正。

textView.autocorrectionType = .no

答案 2 :(得分:0)

由于表视图是从ScrollViews继承的,因此您可以重写scrollview方法“ scrollViewWillEndDragging”并调用endEditing(true)

  override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
                self.view.endEditing(true)

        }

,您甚至可以做更多 您可以使用以下函数签名的速度参数检测用户何时向上或向下滚动:

 override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        if(velocity.y > 0){
            self.view.endEditing(true)
            self.view.layoutIfNeeded()
        }
    }

 override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
            if(velocity.y < 0){
                self.view.endEditing(true)
                self.view.layoutIfNeeded()
            }
        }

答案 3 :(得分:0)

您可以尝试设置空视图textView.inputAccessoryView = UIView()