Keyboard covering UITextView in Notes App

2019-08-22 05:23发布

问题:

Hi guys I am making a notes app and I ran into a big problem. I am using a UITextView as the notepad. When the keyboard comes up it blocks some of the text in the UITextView. I have a input accessory view on the UITextView. I tried to find the answer in the internet and can not find a good answer.Any way to fix it? Here is a picture:

回答1:

You might want to look at modifying the UITextView's contentOffset and contentInset. UITextField is a UIScrollView subclass after all.



回答2:

I decided to subtract the UITextView height by the keyboard height:

NSDictionary* info = [notification userInfo];
kbSIZE = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect newTextViewFrame = self.notesTextView.frame;
newTextViewFrame.size.height -= kbSIZE.size.height;
newTextViewFrame.size.height += self.notesTextView.inputAccessoryView.frame.size.height;
self.notesTextView.frame = newTextViewFrame;


回答3:

You have to set contentInset and scrollIndicatorInsets to the UIEdgeInsets of the keyboard height. The contentInset value makes the scrolling height taller, but allows you to still scroll content under the keyboard. The scrollIndicatorInsets makes the scroll indicators stop at the bottom of the keyboard.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

    self.textView.contentInset = contentInsets;
    self.textView.scrollIndicatorInsets = contentInsets;
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
    self.textView.contentInset = UIEdgeInsetsZero;
    self.textView.scrollIndicatorInsets = UIEdgeInsetsZero;
}