微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。