2013-01-05 95 views
2

我有多个文本字段,当我专注于文本框时,它会自动向上滚动并且键盘隐藏文本字段。当焦点和键盘隐藏文本字段时滚动到文本字段

任何想法如何在点击时将文本字段滚动到焦点字段?

- (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]; 
    } 
} 

回答

1

,你贴我觉得是从苹果的文档,它假设一个基本观点分层结构,同时一个UIScrollView(或其子类之一,像UITableView中)的一个充满整个屏幕的代码片段。如果您的视图布局更加复杂,或者您需要支持多个方向,则文本字段将不会滚动为可见,因为矩形计算会出错。您需要稍微调整一下代码,我的建议是您以这种方式处理问题:

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

在代码:我已经使用了方便的UIScrollView的scrollRectToVisible功能抽象最终滚动操作尽可能

- (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]; 
} 

通知。

+0

KeyboardWasShown在文本框焦点时未被调用。 – user1688346

+0

您是否在viewDidLoad方法中调用了registerForKeyboardNotifications? –

+0

是的。我确实注册了它。 – user1688346

相关问题