【问题标题】:UIScrollView height with keyboard带键盘的 UIScrollView 高度
【发布时间】:2016-10-21 17:59:54
【问题描述】:

我有一个 UIScrollView,里面有一个 UIView,基于一篇文章如何在输入字段中使用自动布局。文章解释了如何将 UIView 设置为与普通视图相同的高度,更多信息:https://www.natashatherobot.com/ios-autolayout-scrollview/

现在我的内容视图中有多个 UITextField。当我按下 UITextField 时,我希望我的视图显示键盘并滚动到 UITextField。起初,我尝试了多个使用的方法,但问题是,在屏幕底部,我从视图底部得到了很多不需要的空白空间。我尝试的方法是使用以下几行:

- (void)keyboardWillHide:(NSNotification *)notification {
    [[self scrollView] setContentInset:UIEdgeInsetsMake([[self scrollView] contentInset].top, 0.0f, 0.0f, 0.0f)];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    [[self scrollView] setContentInset:UIEdgeInsetsMake([[self scrollView] contentInset].top, 0.0f, [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height, 0.0f)];
}

为了改变这一点,我认为将底部更改为更合适的值就可以了,但是当我这样做时,当我按下它时,视图将不再滚动到 UITextField。我可以手动滚动该字段。我实现这一点的方法是使用以下内容:

- (void)keyboardWillShow:(NSNotification *)notification {
    float keyboard = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    float offset = [[[[self contentView] subviews] lastObject] frame].size.height + [[[[self contentView] subviews] lastObject] frame].origin.y + 30.0f;

    if ([[self contentView] frame].size.height - [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height < offset) {
        [[self scrollView] setContentInset:UIEdgeInsetsMake([[self scrollView] contentInset].top, 0.0f, offset - ([[self view] frame].size.height - keyboard), 0.0f)];
    }
}

有谁知道我如何在滚动时忽略 UITextFields 下的空白区域,但允许滚动视图滚动到选定的 UITextField。我尝试使用方法 scrollRectToVisible,但这并没有做任何事情。

【问题讨论】:

  • 使用github.com/michaeltyson/TPKeyboardAvoiding库,超级简单。只需将 TPKeyboardAvoidingScrollView 设置为滚动视图的自定义类,它会处理您在显示键盘时需要管理的所有内容。
  • 感谢图书馆,但是有没有办法在不包括图书馆的情况下做到这一点?我尝试使用代码,滚动视图会滚动到输入字段,但是当我使用库中提供的代码时,滚动视图会向上跳并从顶部滚动。

标签: ios objective-c uiscrollview keyboard uitextfield


【解决方案1】:

我发现上面的答案已经过时了。滚动时也不完美。

这是一个快速版本。

它将在文本字段的正下方滚动,没有多余的空间。它会恢复到它第一次出现时的样子。

//add observer
override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ARVHttpPlayVC.keyboardDidShow(_:)), name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ARVHttpPlayVC.keyboardDidHide(_:)), name: UIKeyboardDidHideNotification, object: nil)
}

func keyboardDidShow(notification: NSNotification) {
    let userInfo: NSDictionary = notification.userInfo!
    let keyboardSize = userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue.size

    //calculate the space it needs to show the firstResponder textField completely
    var difference: CGFloat
    if inputTextField1.isFirstResponder() == true {
        difference = keyboardSize.height - (self.view.frame.height - inputTextField1.frame.origin.y - inputTextField1.frame.size.height)
    } else if inputTextField2.isFirstResponder() == true  {
        difference = keyboardSize.height - (self.view.frame.height - inputTextField2.frame.origin.y - inputTextField2.frame.size.height)
    } else {
        difference = keyboardSize.height - (self.view.frame.height - inputTextField3.frame.origin.y - inputTextField3.frame.size.height)
    }
    //if the textField frame under the keyboard, so scroll up to show it
    if difference > 0 {
        var contentInset:UIEdgeInsets = self.scrollView.contentInset
        contentInset.bottom = difference
        self.scrollView.contentInset = contentInset

        let scrollPoint = CGPointMake(0, difference)
        self.scrollView.setContentOffset(scrollPoint, animated: true)
    }

}

func keyboardDidHide(notification: NSNotification) {
    let contentInset:UIEdgeInsets = UIEdgeInsetsZero
    self.scrollView.contentInset = contentInset
}

//remove observer
deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 2023-03-31
    • 2017-02-10
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    相关资源
    最近更新 更多