- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的ViewController View 包含scrollView及其父 View 的边界,并且scrollView的底部有一个tableView作为它的 subview 。 TableView 高度约束设置为其内容大小高度,其上方有文本字段。因此,当用户触摸文本字段时,键盘就会出现。为了让textField可见,我想向上滚动scrollView,但通常没有足够的空间,所以我想增加tableView的高度。问题是它的行为就像首先滚动scrollView,然后滚动tableViewConstraint
已设置,因此当没有空间时它永远不会滚动足够。我可以“ dispatch ”吗 tableViewConstraint.constant = keyboardHeight
以某种方式,它将首先在 UI 上执行。
override func viewDidLoad() {
tableViewConstraint.constant = TableView.contentSize.height
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
...
}
@objc func keyboardWillShow(notification:NSNotification){
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
tableViewConstraint.constant = keyboardHeight
let boundsOfOffsetView = scrollView.bounds.offsetBy(dx: CGFloat(0), dy: keyboardHeight)
scrollView.scrollRectToVisible(boundsOfOffsetView, animated: true)
}
}
最佳答案
您可以在 animate(withDuration:animations:completion:)
中进行布局更改阻止并调用 layoutIfNeeded()
以强制在您选择的时间发生更改。我会尝试这样的开始:
// Within your if statement…
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
UIView.animate(withDuration: 1.0/3.0) {
tableViewConstraint.constant = keyboardHeight
scrollView.layoutIfNeeded()
}
let boundsOfOffsetView = scrollView.bounds.offsetBy(dx: CGFloat(0), dy: keyboardHeight)
scrollView.scrollRectToVisible(boundsOfOffsetView, animated: true)
关于ios - KeyboardWillShow 中设置的执行顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47581954/
我的ViewController View 包含scrollView及其父 View 的边界,并且scrollView的底部有一个tableView作为它的 subview 。 TableView 高
我有一个聊天 View ,我通过更改 View 的高度来使用键盘将 textView 向上移动。但是,当我将键盘类型更改为表情符号,并返回到常规键盘时,UIKeyboardWillShowNotifi
当使用自定义键盘时,keyboardWillShow 运行两次(正常行为),第一次高度为 0,但第二次是正确的高度,在我的例子中是 667。问题是这只是真实的第二次显示 viewController。
我遇到了“keyboardWillShow”触发两次,但“keyboardWillHide”调用一次的问题。 这里是 an example一旦“keyboardWillShow”触发,我就会在其中打印
我知道这是应该发生的事情,但它给我带来了我不知道如何解决的问题。 我想在键盘显示时向上移动我的 View ,以便我的文本字段保持可见。 我的文本字段有数字键盘。 我使用通知和 keyboardWill
这里有问题,当我调试我的程序时,我发现 keyboardWillShow 函数不是每次都响应。只是第一次,它会被程序调用。这是我的代码,我不知道我的代码有什么问题,但是,当键盘第一次出现时,功能运行良
我有一个带有键盘通知的 View ,例如 keyboardWillShow 和 keyboardWillHide 我使用的所有通知处理代码都取自苹果的示例代码"KeyboardAccessory" 当
我的代码没有显示任何错误,但是当我启动模拟器时,应用程序崩溃了。我似乎找不到问题,但当我实现“keyboardWillShow”时它开始崩溃。 override func viewDidLoad()
我的代码在第一次调用键盘时似乎运行良好。第二次调用键盘时,帧大小没有增加。 这是我的代码: @objc func keyboardWillShow(sender: NSNotification) {
我试图获取键盘测量值,但在旋转过程中,我总是在打印部分遇到 2 个键盘测量到水平位置的通知和三个到垂直位置的通知。 override func viewDidLoad() { super.
目前我有一个 View 在键盘显示或隐藏时使用键盘进行动画处理。我添加了一个手势识别器,这样当用户点击键盘时它就会消失。 我遇到的问题是,如果用户在键盘出现时点击以降低键盘,键盘就会消失,我的 Vie
我已经实现了 keyboardWillShow: 和 keyboardWillHide: 通知。我正在注册通知,在直接触摸 UITextField 上的屏幕后它们可以正常工作,但是有一个问题。在我的
我有下面这段代码,它在调用 keyboardWillShowNotification 时运行: func keyboardWillShow(_ notification: Notification)
我有一个问题。 keyboardDidShow 和 keyboardWillShow 有什么区别?如果我把它放在我的 viewDidLoad 中: [[NSNotificationCenter def
我有一个要求,我需要在其中添加一个与设备键盘具有相同高度的 View 。 由于不同的设备有不同的高度,有没有办法在不注册键盘通知的情况下获取设备的键盘高度? 如果用户在应用中的任意位置打开键盘,我可以
我有一个带有自定义 View 的 xib 文件,上面有很多文本字段 subview 。我已经将每个文本字段的委托(delegate)设置为文件所有者,并且能够成功地使用 texfield 委托(del
序言 所以我有一个以聊天部分为特色的应用程序,我正在同步键盘隐藏和显示的动画与聊天输入的上升和下降。 这是我使用的代码: 显示: - (void) keyboardWillShow:(NSNotifi
我有一段代码可以在 Swift 2 中运行,我尝试使用 Xcode 将代码更新到最新版本,我修复了除了两个问题之外的所有问题。 我有这段代码: let loginvc: LoginVC = self.
升级到0.26.0-rc版本后,在iOs上这一行: DeviceEventEmitter.addListener('keyboardWillShow', (e)=>this.updateKeyboar
我有一个 UITableView,它几乎填满了我的整个 UIViewController,我在底部有一个 UIView,其中包含一个按钮和一个文本字段。 当我点击文本字段时,我希望 UIView 和
我是一名优秀的程序员,十分优秀!