【问题标题】:Textfield inside a scroll view only moves up after keyboard appears IOS滚动视图中的文本字段仅在键盘出现 IOS 后向上移动
【发布时间】:2018-12-26 23:53:18
【问题描述】:

我有以下问题:我有一个scrollView,里面有一个textField。我需要当我点击textField 时,键盘会出现,textField(在滚动条内)会立即(同时)向上移动键盘显示。现在textField 出现但有延迟,比如键盘显示后几毫秒,如下所示:

更新:

我已经重写了这样的键盘处理方法:

override func keyboardWillShow(_ notification: Notification!) {
    guard let userInfo = notification.userInfo, let frame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
        return
    }

    // this constraint -> viewContainerButtonBottomConstraint 
    //refers to the button that must have above the keyboard 
    //when keyboard shows up.   
    self.viewContainerButtonBottomConstraint.constant = frame.height
    UIView.animate(withDuration: 0.3, animations: {
        self.view.layoutIfNeeded()
    }, completion: nil)

    let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: frame.height, right: 0)
    scrollView.contentInset = contentInset

    if #available(iOS 11.0, *) {
        self.viewContainerButtonBottomConstraint.constant -= self.view.safeAreaInsets.bottom
    }
}

override func keyboardWillHide(_ notification: Notification!) {
    scrollView.contentInset = UIEdgeInsets.zero

    self.viewContainerButtonBottomConstraint.constant = 0

    UIView.animate(withDuration: 1, animations: {
        self.view.layoutIfNeeded()
    }, completion: nil)
} 

观察者的注册是在父类中进行的:

- (void)addNotificationKeyboard {
    if (kKeyboardNotificationsShowAvailable) {
     [[NSNotificationCenter defaultCenter] addObserver:self

     selector:@selector(keyboardWillShowNotification:)

     name:UIKeyboardWillShowNotification
                                               object:nil];
   }
   if (kKeyboardNotificationsHideAvailable) {
     [[NSNotificationCenter defaultCenter] addObserver:self

     selector:@selector(keyboardWillHideNotification:)

     name:UIKeyboardWillHideNotification
                                               object:nil];
}

}

并且方法keyboardWillShowNotification 和keyboardWillHideNotification 也在父类中,它们像我上面展示的那样被覆盖:

- (void)keyboardWillShowNotification:(NSNotification*)notification
{
   NSDictionary* keyboardInfo = [notification userInfo];
   NSValue* keyboardFrameBegin = [keyboardInfo 
   valueForKey:UIKeyboardFrameEndUserInfoKey];
   self.keyboardHeight = [keyboardFrameBegin CGRectValue].size.height;
}

- (void)keyboardWillHideNotification:(NSNotification*)notification
{
    if (self.view.frame.origin.y != 0) {
    if (self.tabBarController) {
        if (!(self.view.frame.origin.y == 64))
            [UIUtilities moveView:self.view newYposition:-64];
    }
    else
        [UIUtilities moveView:self.view newYposition:0];
    }
    [self.view endEditing:YES];
}

提前感谢您的回答!

【问题讨论】:

  • 内嵌图片而不是链接。
  • 这似乎是大多数 Apple 应用程序的默认行为,似乎是故意的。您可以计算所需的偏移量并将其设置为滚动或表格视图的contentInset.bottom 属性,但我强烈建议您保留操作系统实现。
  • 我认为您可能使用的是keyBoardDidShow 而不是keyboardWillShow 通知。
  • @RakeshaShastri 我正在使用keyboardWillShow.. :(,虽然我尝试覆盖这两种方法但仍然相同
  • 请显示您的通知注册码和选择器方法。

标签: ios swift uiscrollview keyboard uitextfield


【解决方案1】:

你想要的并不难做到。在显示键盘之前会发送一个名为UIKeyboardWillShow 的系统通知。注意那个。它的用户信息包含有关键盘在屏幕上时的大小和位置的所有信息,以及将用于到达那里的动画。您所要做的就是立即触发匹配的动画来移动或调整您的显示器。然后两个动画将同时运行。

【讨论】:

  • 我已经更新了我的问题以显示键盘处理的覆盖方法,我不明白你指的是什么匹配的动画。
【解决方案2】:

这就是我所拥有的:

@objc func adjustForKeyboard(notification: Notification)
{
    let userInfo = notification.userInfo!
    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)

    if notification.name == Notification.Name.UIKeyboardWillHide
    {
        self.scrollForm.contentInset = UIEdgeInsets.zero
    }
    else
    {
        self.scrollForm.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
    }
    self.scrollForm.scrollIndicatorInsets = self.scrollForm.contentInset
    //let selectedRange = self.scrollForm.selectedRange
    //self.scrollForm.scrollRangeToVisible(selectedRange)
}

并在viewDidLoad()内:

    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: Notification.Name.UIKeyboardWillHide, object: nil)
    notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)

这可能与您所拥有的类似,请尝试将您的覆盖替换为上述内容。

【讨论】:

  • 标签上写着'Swift',但你似乎使用了很多Objective-C。
猜你喜欢
  • 2013-11-11
  • 2017-04-28
  • 2015-11-29
  • 2020-05-21
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多