0

我在 aUITextField中嵌入了一个UIScrollView,并且我希望文本字段在键盘处于活动状态时向上滚动以可见。我在How to make a UITextField move up when keyboard is present中尝试了最佳答案?但它似乎不适用于自动布局,所以我从苹果文档中获取了代码。这几乎可以工作,但在用户实际输入一些数据之前,它不会滚动视图,而不是当键盘实际出现时。

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

// 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];
}
4

2 回答 2

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];
    }
}
于 2013-09-10T09:30:00.787 回答
0

尝试这个,

视图控制器.h

bool keyboardIsShown;
UITapGestureRecognizer *tapGesture;

视图控制器.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];
  }
于 2013-09-10T09:57:36.457 回答