TPKeyboardAvoiding
TPKeyboardAvoiding copied to clipboard
ios6 AutoLayout Support
It seems that the view does not resize properly when using ios 6 and auto layout.
I have the same issue too.
me 2
It is really an issue between UIScrollView and autolayout. One way to reduce (not totally eliminate) it is by adding a UIView as a subview to the scrollview. You can then put your objects in the UIView and expect a more normal autolayout behavior.
I'm not sure if I understand this issue, but I am using iOS 6 with Autolayout in Storyboard, and I get extra space at the bottom after the keyboard is dismissed. Also, using a tab bar controller and going back and forth between different views also seems to mess up the scrollview's scroll position and content size.
I fixed all of these problems by using the following implementation:
Setup these properties in the view controller that contains the TPKeyboardAvoidingScrollView
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *scrollView;
// Subview of the scrollview that holds all your textfields, labels, etc
@property (weak, nonatomic) IBOutlet UIView *formView;
// Stores current scroll position
@property (nonatomic) CGPoint scrollOffset;
Implement these methods in the view controller that contains the TPKeyboardAvoidingScrollView
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Set and enable scrollable area
[self.scrollView setContentSize:self.formView.frame.size];
// Load last remembered scroll position
self.scrollView.contentOffset = self.scrollOffset;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// Store current scroll position when leaving this view
self.scrollOffset = self.scrollView.contentOffset;
// Reset content offset to fix TPKeyboardAvoidingScrollView and Autolayout incompatibilities,
// and prevent the content subview from going outside the scrollview and its scrollable area
self.scrollView.contentOffset = CGPointZero;
}
Replace a line of code for method "keyboardWillHide" in TPKeyboardAvoidingScrollView.m
Change...
self.contentInset = _priorInset;
to...
self.contentInset = UIEdgeInsetsMake(_priorInset.top, _priorInset.left, 0, _priorInset.right);
Doing some NSLog debugging showed that the scrollview's bottom inset remains at 162 after the keyboard is dismissed, thus we get the extra space at the bottom.
Now, everything is working as expected. I can place a view of any height inside the scrollview, and have it scroll and show/hide keyboard beautifully. I can also navigate through navigation controller stacks, or switch views using the tab bar controller, and it won't mess up the scrollview.
I am not an expert programmer by any means, so I could be wrong about whether TPKeyboardAvoiding is compatible with Autolayout, as well as many other things. Please kindly let me know if I've done anything poorly.
Big thanks to Michael Tyson, I love your API's and The Amazing Audio Engine!
P.S. I have yet to test this on iOS 7, iPad, and landscape orientation.