Created
January 31, 2016 16:20
-
-
Save AshokEmrys/594cb904096b59211325 to your computer and use it in GitHub Desktop.
IOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -(void)setContentOffset:(CGPoint)contentOffset | |
| { | |
| const CGSize contentSize = self.contentSize; | |
| const CGSize scrollViewSize = self.bounds.size; | |
| //automatically center scrollview content when the scroll size is within bounds | |
| if (contentSize.width + self.contentInset.left + self.contentInset.right < scrollViewSize.width) { | |
| contentOffset.x = - (scrollViewSize.width - contentSize.width) / 2.0; | |
| } | |
| if (contentSize.height + self.contentInset.top + self.contentInset.bottom < scrollViewSize.height) { | |
| contentOffset.y = - (scrollViewSize.height - contentSize.height) / 2.0; | |
| } | |
| [super setContentOffset:contentOffset]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ViewController.h" | |
| #import "UIResponder+FirstResponder.h" | |
| #import "PXScrollView.h" | |
| @interface ViewController () | |
| @property (weak, nonatomic) IBOutlet PXScrollView *scrollView; | |
| @property(nonatomic, weak) UIView *activeTextInput; | |
| @end | |
| @implementation ViewController | |
| - (void)viewDidLoad { | |
| [super viewDidLoad]; | |
| // Do any additional setup after loading the view, typically from a nib. | |
| [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil]; | |
| [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
| } | |
| - (void)didReceiveMemoryWarning { | |
| [super didReceiveMemoryWarning]; | |
| // Dispose of any resources that can be recreated. | |
| } | |
| #pragma mark - KeyboardManagement | |
| - (void)keyboardWasShown:(NSNotification *)notification | |
| { | |
| NSDictionary *userInfo = [notification userInfo]; | |
| CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; | |
| UIEdgeInsets contentInset = UIEdgeInsetsMake(0, 0, kbSize.height, 0); | |
| self.scrollView.contentInset = contentInset; | |
| self.scrollView.scrollIndicatorInsets = contentInset; | |
| CGRect scrollViewRect = self.scrollView.frame; | |
| scrollViewRect.size.height -= kbSize.height; | |
| id textInputView = [UIResponder getFirstResponder]; | |
| if ([textInputView isKindOfClass:[UITextField class]] | |
| || | |
| [textInputView isKindOfClass:[UITextView class]]) { | |
| if (!CGRectContainsPoint(scrollViewRect, ((UIView *)textInputView).frame.origin)) { | |
| [self.scrollView scrollRectToVisible:((UIView *)textInputView).frame animated:YES]; | |
| } | |
| } | |
| } | |
| - (void)keyboardWillHide:(NSNotification *)notification | |
| { | |
| UIEdgeInsets contentInset = UIEdgeInsetsZero; | |
| self.scrollView.contentInset = contentInset; | |
| self.scrollView.scrollIndicatorInsets = contentInset; | |
| } | |
| -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event | |
| { | |
| id firstResponder = [UIResponder getFirstResponder]; | |
| if ([firstResponder isKindOfClass:[UITextField class]] || [firstResponder isKindOfClass:[UITextView class]]) { | |
| UITouch *touch = [touches anyObject]; | |
| if ([firstResponder isFirstResponder] && touch.view != firstResponder) { | |
| [firstResponder resignFirstResponder]; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static __weak id _currentFirstResponder; | |
| static BOOL _shouldAutomaticallyDismissKeyboard; | |
| @implementation UIResponder (FirstResponder) | |
| +(id)getFirstResponder | |
| { | |
| _currentFirstResponder = nil; | |
| [[UIApplication sharedApplication] sendAction:@selector(populateFirstResponder) to:nil from:nil forEvent:nil]; | |
| return _currentFirstResponder; | |
| } | |
| - (void)populateFirstResponder | |
| { | |
| _currentFirstResponder = self; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment