swift - iOS 如何防止 ios 自定义导航栏在键盘出现时向上移动

问题描述

当我尝试打开键盘时,我的所有视图都向上滚动。我只需要滚动 tableview 和 textview。换句话说,我需要保持导航栏不向上滚动。

我的视图堆栈是:

查看

  • 导航栏视图
  • tableView
  • 文本视图

enter image description here

enter image description here

 override func viewDidLoad() {
        super.viewDidLoad()
        self.adjustViews()
        
        ChatTableSectionHeaderView.dateFormatter.dateStyle = .medium
        ChatTableSectionHeaderView.dateFormatter.timeStyle = .none
        
        MessageInboundCell.dateFormatter.dateStyle = .none
        MessageInboundCell.dateFormatter.timeStyle = .short

        self.attachButton.isHidden = true
        self.textView.delegate = self
        self.setUpSendButton(isdisabled: !isChatTextValid(text: self.textView.text))
        
        self.tableView.tableFooterView = UIView(frame: .zero)
        
        let igTextNib = UINib(nibName: MessageInboundCell.string,bundle: nil)
        let igImageNib = UINib(nibName: MessageImageInboundCell.string,bundle: nil)
        let sectionHeaderView = UINib(nibName: ChatTableSectionHeaderView.string,bundle: nil)
        
       
        tableView.register(igTextNib,forCellReuseIdentifier: MessageInboundCell.string)
        tableView.register(igImageNib,forCellReuseIdentifier: MessageImageInboundCell.string)
        tableView.register(sectionHeaderView,forheaderfooterViewReuseIdentifier: ChatTableSectionHeaderView.string)
        
        tableView.delegate = self
        tableView.dataSource = self
        
        registerForKeyboardWillShowNotification(tableView)
        registerForKeyboardWillHideNotification(tableView)

        
    }
}

扩展

func registerForKeyboardWillShowNotification(_ scrollView: UIScrollView,usingBlock block: ((CGSize?) -> Void)? = nil) {
            _ = NotificationCenter.default.addobserver(forName: UIResponder.keyboardWillShowNotification,object: nil,queue: nil,using: { notification -> Void in
                let userInfo = notification.userInfo!
                let keyboardSize = (userInfo[UIResponder.keyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue.size
                let contentInsets = UIEdgeInsets(top: scrollView.contentInset.top,left: scrollView.contentInset.left,bottom: keyboardSize.height,right: scrollView.contentInset.right)

                scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets)
                block?(keyboardSize)
            })
        }

        func registerForKeyboardWillHideNotification(_ scrollView: UIScrollView,usingBlock block: ((CGSize?) -> Void)? = nil) {
            _ = NotificationCenter.default.addobserver(forName: UIResponder.keyboardWillHideNotification,bottom: 0,right: scrollView.contentInset.right)

                scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets)
                block?(keyboardSize)
            })
        }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)