【问题标题】:Scroll to textfield when focus and keyboard hide text field当焦点和键盘隐藏文本字段时滚动到文本字段
【发布时间】:2013-01-05 00:36:47
【问题描述】:

我有多个文本字段,当我关注文本框时,它会自动向上滚动并且键盘隐藏文本字段。

知道单击时如何将文本字段滚动到焦点字段吗?

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

【问题讨论】:

    标签: objective-c ios keyboard uitextfield


    【解决方案1】:

    我认为您发布的代码 sn-p 是 Apple 文档中的代码,它假定基本视图层次结构具有 UIScrollView(或其子类之一,如 UITableView)填充整个屏幕。如果您的视图布局更复杂,或者您需要支持多个方向,则文本字段将不会滚动到可见,因为矩形计算将是错误的。您需要稍微调整一下代码,我的建议是您以这种方式解决问题:

    滚动视图的新 contentInsent 高度应该等于键盘和滚动视图之间相交矩形的高度

    在代码中:

    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        CGRect kbRawRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];    
        CGRect scrollViewFrame = [self.scrollView.window convertRect:self.scrollView.frame fromView:self.scrollView.superview];
    
        // Calculate the area that is covered by the keyboard
        CGRect coveredFrame = CGRectIntersection(scrollViewFrame, kbRawRect);
        // Convert again to window coordinates to take rotations into account
        coveredFrame = [self.scrollView.window convertRect:self.scrollView.frame fromView:self.scrollView.superview];
    
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, coveredFrame.size.height, 0.0);
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;
    
        // If active text field is hidden by keyboard, scroll it so it's visible
        CGRect activeFieldRect = [self.activeField convertRect:self.activeField.bounds toView:self.scrollView];
       [self.scrollView scrollRectToVisible:activeFieldRect animated:YES];
    }
    

    请注意,我已经使用了方便的 UIScrollView 的 scrollRectToVisible 函数来尽可能抽象最终的滚动操作。

    【讨论】:

    • KeyboardWasShown 在文本框焦点时没有被调用。
    • 你在 viewDidLoad 方法中调用 registerForKeyboardNotifications 了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2014-03-18
    • 2011-01-19
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    相关资源
    最近更新 更多