From 93db1e2ce763e68dcbaa786991e2a554a50489db Mon Sep 17 00:00:00 2001 From: Eddie Ehlin Date: Tue, 8 Jan 2013 22:07:41 +0100 Subject: Added keyboard fix in BeanCollectionInfo view, so that it doesn't gets displayed over the textview. --- .../UserInterfaceState.xcuserstate | Bin 31984 -> 30104 bytes iBean/iBean/BeanCollectionInfoViewController.h | 6 +- iBean/iBean/BeanCollectionInfoViewController.m | 48 ++++++++++ iBean/iBean/iPhoneStoryboard.storyboard | 97 +++++++++++++-------- 4 files changed, 112 insertions(+), 39 deletions(-) (limited to 'iBean') diff --git a/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate b/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate index 7042894..a31d1c9 100644 Binary files a/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate and b/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/iBean/iBean/BeanCollectionInfoViewController.h b/iBean/iBean/BeanCollectionInfoViewController.h index 391869d..815a7b1 100644 --- a/iBean/iBean/BeanCollectionInfoViewController.h +++ b/iBean/iBean/BeanCollectionInfoViewController.h @@ -9,7 +9,7 @@ #import @class BeanCollection; -@interface BeanCollectionInfoViewController : UIViewController +@interface BeanCollectionInfoViewController : UIViewController @property (nonatomic, strong) BeanCollection *beanCollection; @property (nonatomic, assign) BOOL editMode; @@ -22,11 +22,15 @@ /* UI Outlets */ @property (weak, nonatomic) IBOutlet UITextField *beanCollectionNameTextField; @property (weak, nonatomic) IBOutlet UITextView *beanCollectionNoteTextView; +@property (weak, nonatomic) IBOutlet UIScrollView *beanCollectionScrollView; /* UI Actions */ - (IBAction) beanCollectioNameChanged; - (IBAction) beanCollectionNoteChanged; +- (IBAction) keyboardDisplayed:(NSNotification*)keyboardNotification; +- (IBAction) keyboardHidden:(NSNotification*)keyboardNotification; + /* UITextFieldDelegate */ - (BOOL)textFieldShouldReturn:(UITextField *)textField; diff --git a/iBean/iBean/BeanCollectionInfoViewController.m b/iBean/iBean/BeanCollectionInfoViewController.m index a246b47..39a6422 100644 --- a/iBean/iBean/BeanCollectionInfoViewController.m +++ b/iBean/iBean/BeanCollectionInfoViewController.m @@ -35,6 +35,23 @@ { [super viewWillAppear:animated]; [self initViewController]; + + //Register keyboard events + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardDisplayed:) + name:UIKeyboardDidShowNotification object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardHidden:) + name:UIKeyboardWillHideNotification object:nil]; +} + +- (void) viewWillDisappear:(BOOL)animated +{ + [super viewWillDisappear:animated]; + + //Unregister keyboard events + [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)didReceiveMemoryWarning @@ -55,6 +72,7 @@ } //Purpose: Hide keyboard when active control looses focus. +/* - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; @@ -70,6 +88,7 @@ [super touchesBegan:touches withEvent:event]; } +*/ /***************************************************** Utility methods @@ -120,6 +139,35 @@ } } +- (void) keyboardDisplayed:(NSNotification *)keyboardNotification +{ + //Make sure that beanCollectionTextView is accessable even with keyboard visible. + //See: https://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html + NSDictionary* info = [keyboardNotification userInfo]; + CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; + + UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); + self.beanCollectionScrollView.contentInset = contentInsets; + self.beanCollectionScrollView.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, self.beanCollectionNoteTextView.frame.origin) ) { + CGPoint scrollPoint = CGPointMake(0.0, self.beanCollectionNoteTextView.frame.origin.y-kbSize.height); + [self.beanCollectionScrollView setContentOffset:scrollPoint animated:YES]; + }*/ +} + +- (void) keyboardHidden:(NSNotification *)keyboardNotification +{ + UIEdgeInsets contentInsets = UIEdgeInsetsZero; + self.beanCollectionScrollView.contentInset = contentInsets; + self.beanCollectionScrollView.scrollIndicatorInsets = contentInsets; +} + /* UITextFieldDelegate */ #pragma mark - UITextFieldDelegate methods diff --git a/iBean/iBean/iPhoneStoryboard.storyboard b/iBean/iBean/iPhoneStoryboard.storyboard index 304377c..22197b1 100644 --- a/iBean/iBean/iPhoneStoryboard.storyboard +++ b/iBean/iBean/iPhoneStoryboard.storyboard @@ -170,49 +170,66 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - - - - - - - - - - - - - + + + + @@ -230,6 +247,7 @@ + @@ -460,8 +478,11 @@ + + + -- cgit v1.2.3