在文本字段的上方移动文本字段时发出问题

时间:2014-11-19 06:05:05

标签: ios uitextview uitoolbar uikeyboard

我已经放置了一个工具栏,键盘上方有一个按钮可以将其关闭。我在向上和向下移动文本视图时遇到问题。如果textview被键盘滚动覆盖,则rect工作正常。有时文本视图不会被键盘覆盖,但工具栏会覆盖它。我希望textview在这种情况下也向上移动,它应该在textview上移动到我的工具栏上方。目前,当工具栏覆盖时,我的textview不会向上移动。怎么做到这一点?

// 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);
    scroll.contentInset = contentInsets;
    scroll.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        [self.scroll scrollRectToVisible:activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scroll.contentInset = contentInsets;
    scroll.scrollIndicatorInsets = contentInsets;
}
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

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

}

//To add toolbar above textview
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30)];
            numberToolbar.barStyle = UIBarStyleBlackTranslucent;
            numberToolbar.items = [NSArray arrayWithObjects:
                                   [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                   [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                   [[UIBarButtonItem alloc]initWithTitle:AGLocalizedString(@"key_done",nil) style:UIBarButtonItemStyleDone target:self action:@selector(returnKeyboardOnDone)],
                                   nil];
            [numberToolbar sizeToFit];
            subTitleTxtFld.inputAccessoryView = numberToolbar;
            compaintSummaryTxt.inputAccessoryView = numberToolbar;
            aeTxtView.inputAccessoryView = numberToolbar;

2 个答案:

答案 0 :(得分:0)

使用工具栏的参考变量,代替键盘大小高度,保持(键盘大小高度+工具栏高度)。这将完成工作。

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height + toolBar.frame.size.height, 0.0);

答案 1 :(得分:0)

滚动caretRect。而不是制作矩形并滚动它。

CGRect caretRect = [self.textView caretRectForPosition:self.textView.selectedTextRange.end];
// add some extra space to scroll
caretRect.size.height += 7;
[self.textView scrollRectToVisible:caretRect animated:YES];