UITextField在编辑之前不会滚动到视图中

时间:2013-09-10 08:40:41

标签: ios uiscrollview

我在UITextField中嵌入了UIScrollView,我希望文本字段向上滚动,以便在键盘处于活动状态时可见。我在How to make a UITextField move up when keyboard is present?中尝试了最常见的答案,但似乎它不适用于自动布局,因此我从apple docs获取了代码。这几乎可以工作,但它不会滚动视图,直到用户实际输入一些数据,而不是键盘实际出现。

这就是我正在做的事情 - 有什么明显的错误吗?

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(keyboardWillShow:)
                                               name:UIKeyboardWillShowNotification object:nil];

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

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillShow:(NSNotification*)aNotification
{
  NSDictionary* info = [aNotification userInfo];
  CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

  UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.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
  // Your application might not need or want this behavior.
  CGRect aRect = self.view.frame;
  aRect.size.height -= kbSize.height;
  if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
    [self.scrollView scrollRectToVisible:self.activeField.frame animated:YES];
  }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.3];
  UIEdgeInsets contentInsets = UIEdgeInsetsZero;
  self.scrollView.contentInset = contentInsets;
  self.scrollView.scrollIndicatorInsets = contentInsets;
  [UIView commitAnimations];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
  NSLog(@"textFieldDidEndEditing");
  self.activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  NSLog(@"textFieldDidEndEditing");
  self.activeField = nil;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self registerForKeyboardNotifications];
}

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

    -(void)keyboardWillShow:(id)sender
    {

            if ([orientation==@"Landscape"])
                [loginScrollView setContentSize:CGSizeMake(480, 620)];
            else
                [loginScrollView setContentSize:CGSizeMake(320, 670)];
    }


 and while editing the text use this 

- (void)textFieldDidBeginEditing:(UITextField *)textField {

     if ([orientation == @"Landscape"]) {
        if (textField == usernameTextField)
            [loginScrollView setContentOffset:CGPointMake(xValue, 200) animated:YES];
        if (textField == passwordTextField)
            [loginScrollView setContentOffset:CGPointMake(xValue, 220) animated:YES];
    }
    else{
        if (textField == usernameTextField)
            [loginScrollView setContentOffset:CGPointMake(xValue, 168) animated:YES];
        if (textField == passwordTextField)
            [loginScrollView setContentOffset:CGPointMake(xValue, 172) animated:YES];
    }
}

答案 1 :(得分:0)

试试这个,

ViewController.h

bool keyboardIsShown;
UITapGestureRecognizer *tapGesture;

ViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];

scrollView.contentSize =CGSizeMake(0, self.view.frame.size.height+50);

// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:self.view.window];
keyboardIsShown = NO;

}


- (void)viewDidUnload
 {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillShowNotification
                                              object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];
}

///// KEYBOARD

- (void) moveView:(float) yPos
 {
[scrollView setContentOffset:CGPointMake(0, yPos) animated:YES];
 }


- (void)keyboardWillHide:(NSNotification *)n
{
keyboardIsShown = NO;
[self.view removeGestureRecognizer:tapGesture];
[self moveView:0.0];
}

- (void)keyboardWillShow:(NSNotification *)n
{
  if (keyboardIsShown)
{
    return;
    }

keyboardIsShown = YES;

[self.view addGestureRecognizer:tapGesture];

    [self moveView:255.0];
}

// method to hide keyboard when user taps on a scrollview
 -(void)dismissKeyboard
  {
[self.view endEditing:YES];
  }