1

当显示键盘时,我需要将整个向上推(它有滚动视图),但似乎无法使用此代码。所有代码都被击中,但它并没有推高视图。我在这里按照示例进行操作。 http://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7

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

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

}

// Called when the UIKeyboardDidShowNotification is sent.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}


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

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.activeField = nil;
}
- (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);
    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.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}
4

3 回答 3

3

我想当用户单击文本字段时会出现键盘吗?您必须对 UITextField 使用委托的方法。

在您的 viewController.h 添加 UITextFieldDelegate 并在您的 .m 插入:

   - (void)textFieldDidBeginEditing:(UITextField *)textField
 {
  [self animateTextField: textField up: YES];
 }


  - (void)textFieldDidEndEditing:(UITextField *)textField
 {
   [self animateTextField: textField up: NO];
 }

 - (void) animateTextField: (UITextField *)textField up: (BOOL) up
{
const int movementDistance = 140; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed

int movement = (up ? -movementDistance : movementDistance);

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}

还添加这些方法:

 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   [yourTextField resignFirstResponder];
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField {
 [yourTextField resignFirstResponder];
 Return YES;
}
于 2013-08-09T07:53:07.317 回答
1

您可以在视图中使用 scrollView。然后将所有其他子视图放在scrollView中,然后在“textfieldShouldBeginEditing”方法中设置scrollView的内容偏移量。

[scrollView setContentOffset:CGPointMake(0,160)]; // this is just an example

希望这会帮助你。

于 2013-08-09T08:06:29.053 回答
0

我有同样的问题!相反,我决定使用表格视图。在 TableViewCells 中插入你的 UITextFields,当键盘出现时它会自动向上滚动!

于 2013-08-08T22:06:57.287 回答