0

我正在制作一个包含多个的应用程序,UITextField并且UITextView TheUITextView位于屏幕底部,每当键入开始时,键盘都会阻止UITextView

当键盘出现在屏幕上时,我将如何向上移动表单的视图?然后当键盘消失时再次向下移动?

4

3 回答 3

1

最好的答案是尽量避免这样做。

但是,如果您将内容放在 UIScrollView 或 UITableView 中,您可以在输入成为第一响应者时滚动到输入。

于 2013-04-11T02:21:29.967 回答
0

确保在你的类中实现 UITextFieldDelegate。这些委托方法应该对名为 activeField 的文本字段起作用:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [activeField setDelegate:self];    
    [self configureView];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return TRUE;
}

// Called when the UIKeyboardDidShowNotification is sent.
- (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];
    }
}

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

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

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}
于 2013-04-11T02:37:11.573 回答
0

我喜欢用这个:https ://github.com/michaeltyson/TPKeyboardAvoiding 它对我来说非常好用,而且很容易使用。

于 2013-04-11T02:22:59.617 回答