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

6qfn3psc  于 2023-03-17  发布在  Swift
关注(0)|答案(1)|浏览(271)

当我尝试到打开键盘我的全部视图滚动向上.我只需要到滚动这tableview和textview.换句话说,我需要到保持导航条没有滚动向上.
我的视图堆栈是:
视图

  • 导航栏视图
  • 表格视图
  • 文本视图

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, 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: 0, right: scrollView.contentInset.right)

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

i34xakig1#

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func keyboardWillShow(notification: NSNotification) {
    moveViewWithKeyboard(notification: notification, viewBottomConstraint: self.bcTextView, keyboardWillShow: true)
}

@objc func keyboardWillHide(notification: NSNotification) {
    moveViewWithKeyboard(notification: notification, viewBottomConstraint: self.bcTextView, keyboardWillShow: false)
}

func moveViewWithKeyboard(notification: NSNotification, viewBottomConstraint: NSLayoutConstraint, keyboardWillShow: Bool) {
    // Keyboard's size
    guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
    let keyboardHeight = keyboardSize.height
    
    // Keyboard's animation duration
    let keyboardDuration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
    
    // Keyboard's animation curve
    let keyboardCurve = UIView.AnimationCurve(rawValue: notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! Int)!
    
    // Change the constant
    if keyboardWillShow {
        let safeAreaExists = (self.view?.window?.safeAreaInsets.bottom != 0) // Check if safe area exists
        let bottomConstant: CGFloat = 10
        viewBottomConstraint.constant = keyboardHeight + (safeAreaExists ? 0 : bottomConstant)
    }else {
        viewBottomConstraint.constant = 10
    }
    
    // Animate the view the same way the keyboard animates
    let animator = UIViewPropertyAnimator(duration: keyboardDuration, curve: keyboardCurve) { [weak self] in
        // Update Constraints
        self?.view.layoutIfNeeded()
    }
    
    // Perform the animation
    animator.startAnimation()
}

相关问题