UIScrollView issue on tapping UITextView or UITextField on keyboard presentation
我有一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | - (void)keyboardDidChangeFrame:(NSNotification *)notification { CGRect keyboardEndFrame; [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame]; CGRect screenRect = [[UIScreen mainScreen] bounds]; CGRect intersection; UIView *theFirstResponder = [[UIApplication sharedApplication].keyWindow findFirstResponder]; if ([theFirstResponder isKindOfClass:[UITextView class]]) { if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) { keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 416, keyboardEndFrame.size.height, keyboardEndFrame.size.width); } else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) { keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 0, keyboardEndFrame.size.height, keyboardEndFrame.size.width); } } else keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, keyboardEndFrame.origin.x, keyboardEndFrame.size.height, keyboardEndFrame.size.width); screenRect = CGRectMake(screenRect.origin.y, screenRect.origin.x, screenRect.size.height, screenRect.size.width); if(CGRectEqualToRect(lastKBDRect, keyboardEndFrame)) { return; } lastKBDRect = keyboardEndFrame; if (CGRectIntersectsRect(keyboardEndFrame, screenRect)) { // Keyboard is visible //Convert Frame of the first responder, in context of the view that needs to be shifted UIView *firstResponder = [[UIApplication sharedApplication].keyWindow findFirstResponder]; CGRect theRect = [firstResponder convertRect:firstResponder.frame toView:[UIApplication sharedApplication].keyWindow]; theRect = CGRectMake(theRect.origin.y, theRect.origin.x > 768 ? 750 : theRect.origin.x, theRect.size.height, theRect.size.width); intersection = CGRectIntersection(keyboardEndFrame, theRect); //If intersection is null, then no need to shift anything. Simply return. if(CGRectIsNull(intersection)) { return; } //Shift the view so that the first responder view is completely visible, keeping the constraint that the origin of the first responder view is also visible. //Remember the current offset, so when we shift the view back, we shift it to the proper position. if (!wasContentViewShifted) { lastContentOffset = contentScrollView.contentOffset; lastContentSize = contentScrollView.contentSize; wasContentViewShifted = YES; } CGFloat offset = theRect.origin.y + theRect.size.height - keyboardEndFrame.origin.y; if((theRect.origin.y - offset) < 40) { offset += 42; } [UIView animateWithDuration:0.3f animations:^{ contentScrollView.contentOffset = CGPointMake(0, contentScrollView.contentOffset.y + offset); contentScrollView.contentSize = CGSizeMake(0, lastContentSize.height + (600 - theRect.size.height)); }]; } else { // Keyboard is hidden. Move the view back only if it was shifted. if(wasContentViewShifted) { wasContentViewShifted = NO; [UIView animateWithDuration:0.3f animations:^{ contentScrollView.contentOffset = lastContentOffset; contentScrollView.contentSize = lastContentSize; }]; } } } |
应用程序仅支持横向。我现在面临的问题是
你可以试着用这个来代替复杂的解决方案。这里还可以找到一个更简单的解决方案。
此外,不建议像您所做的那样对框架进行硬编码。有关更多详细信息,请参阅Apple文档。