UITextView keyboardDismissMode Bug

2019-02-11 15:44发布

问题:

I've just found an annoying bug with the new keyboardDismissMode property of the scroll view. When using this with a text view with the value UIScrollViewKeyboardDismissModeInteractive and the keyboard is dismissed the scroll view seems to jump up to the top before it continues to decelerate.

I've filed a bug report with Apple but need a workaround. I've tried the DAKeyboardControl without the new iOS7 support which behind the scenes is using the keyboardDismissMode and it still does it which to me indicates this is a much deeper problem.

Any suggestions?

回答1:

for this issue better you code with scrollviewDelegete and simply mention when you want dismiss keyboard through ResignFirstResponder



回答2:

Does seem to be a bug or just a non-ideal default state. But based on the code in the test project something like the below may work after some finer tuning.

There are two problems with the sample code, one is that you aren't doing anything about the size of the text when the keyboard does appear, so you can't use or see the text under the keyboard. There are other solutions but a quick and dirty solution is to change the frame size (in a submission app I would also grab the animation info and animate the view frame change to match the keyboard animation which is beyond the scope of this question). You do that in 'willShow' or the like, and bring it back in 'didHide' or the like.

Then, the content offset is fudged when its hidden and there does appear to be some strange states while you are dragging it offscreen before and around your callbacks for hiding and scroll view changes. I just save the state and "fix" it once the keyboard goes away and I've updated the text view.

I created a few properties and an outlet in the storyboard to fudge with the text view.

- (void) viewDidLoad
{
    [super viewDidLoad];

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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void) keyboardWillShow:(NSNotification *)notification
{
    NSDictionary * info = [notification userInfo];
    CGSize size = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect rect = self.textView.frame;
    rect.size.height -= size.height;
    self.textView.frame = rect;
}

- (void)keyboardDidHide:(NSNotification *)notification
{
    NSLog(@"====== keyboardDidHide =======");

    NSDictionary * info = [notification userInfo];
    CGSize size = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect rect = self.textView.frame;
    rect.size.height += size.height;
    self.textView.frame = rect;


    self.hidingKeyboard = YES;
}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%f", scrollView.contentOffset.y);
    if(self.hidingKeyboard == YES)
    {
        scrollView.contentOffset = self.lastOffset;
        self.hidingKeyboard = NO;
        NSLog(@"====== reset =======");
    }
    else
        self.lastOffset = scrollView.contentOffset;
}