问题
I have a question regarding the code found in the Apple documentation Moving content located under the keyboard. I have researched this, but I'm looking for clarification on the concepts I am fuzzy on here before I start getting fancy with alternate solutions.
Unfortunately, as soon as the keyboard animation finishes moving the keyboard up, the view moves down and completely out of site. This happens whether the text field is actually the one that gets covered by the keyboard or not. So now I'm back to trying to understand the code provided in the documentation and after attempts changing a variety of parameters I've been unable to get it to work properly.
The pertinent code provided by Apple is:
- (void)keyboardWasShown:(NSNotification*)aNotification
NSLog(@"keyboardWasShown");
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) ) {
NSLog(@"if");
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
NSLog(@"scrollPoint is %@", NSStringFromCGPoint(scrollPoint));
[scrollView setContentOffset:scrollPoint animated:YES];
First of all, before I change anything as described below, here's what I get:
GO[4614:c07] keyboardWasShown
2012-09-08 22:58:23.892 DRO GO[4614:c07] if
2012-09-08 22:58:23.892 DRO GO[4614:c07] scrollPoint is {0, -281}
So, breaking it down:
NSLog just info for me indicating the method was called
NSDictionary - contains info about the keyboard, including its dimensions, such that different keyboard sizes in different devices or orientations will be automatically accounted for.
CGSize - obtaining the actual size from the dictionary
UIEdgeInsets - found reference in UIKit Function Reference - creates an edge inset for button or view where an inset is a margin around the drawing rectangle where each side (left, right, top, and bottom) can have a different value. In this case, setting the bottom edge at the height of the keyboard. Since my view is moving down instead of up, I tried reversing the 'top' and 'bottom' values, thinking maybe they are inverted. Instead of the view scrolling smoothly down with animation, now it just blinks out of sight when the keyboard pops up and I get this:
GO[4829:c07] if 2012-09-08 23:06:57.146 DRO GO[4829:c07] scrollPoint is {0, -281}
scrollView.contentInset: the distance that the content view is inset from the enclosing scroll view. Use this property to add to the scrolling area around the content. The unit of size is points. The default value isUIEdgeInsetsZero Seems like this should be adding area to the bottom of the scroll view to allow it to move up along with the keyboard.
scrollView.scrollIndicatorInset - telling the scroll indicator to move proportionally to the view movement in response to the keyboard
CGRect - creating an aRect object with the view dimensions.
aRect - the height value of the CGRect, reduced by the height of the keyboard. I'm super fuzzy here - Should be the area of the view not covered by the keyboard.
if - if the CGpoint is not (in the rectangle that lies under the keyboard, at a point within the active text field) - which doesn't make sense; the logic here escapes me
NSLog - telling me we got this far
CGScrollPoint - defining the point the view should be scrolling to. Since view origin is upper left corner, subtracting the keyboard height should have it scroll up the distance of the keyboard height, keeping the textfield in view. Just for kicks, when I tried changing it to +kbSizeHeight I got the view disappearing swiftly and: GO[4682:c07] if 2012-09-08 23:02:48.078 DRO GO[4682:c07] scrollPoint is {0, 679}
NSLog - telling me what scroll point the view is to be moved to
[scrollView - telling the scrollView to adjust to the new scrollPoing using animation
回答1:
I don't know how Apple does it, I believe they recommend to put the content into a scrollview then when you tap text field, you tell the scrollview to scroll up so you can see what you're typing.
I am a bit of a lazy person, so all I do is tell the view itself to shift up when user taps on a field that will be hidden by the keyboard.
I usually do this with:
// shift view up
self.view.transform = CGAffineTransformMakeTranslation(0, -someYOffsetValue);
// shift view back down to original value
self.view.transform = CGAffineTransformMakeTranslation(0, 0);
You can wrap this in a UIView animateWithDuration: block for a smooth transition.
来源:https://stackoverflow.com/questions/12345131/moving-content-located-under-the-keyboard