From 8f290f7e11843925780d555e64f2c4f3527877ad Mon Sep 17 00:00:00 2001 From: lijunjie Date: Wed, 1 Jul 2015 21:31:37 +0800 Subject: [PATCH] update Demo with iOS8 update Demo with iOS8 --- .../Classes/BHInputToolbarViewController.h | 6 +- .../Classes/BHInputToolbarViewController.m | 98 +++++-------------- 2 files changed, 25 insertions(+), 79 deletions(-) diff --git a/UIInputToolbarSample/Classes/BHInputToolbarViewController.h b/UIInputToolbarSample/Classes/BHInputToolbarViewController.h index 0771ca6..ae37e9e 100644 --- a/UIInputToolbarSample/Classes/BHInputToolbarViewController.h +++ b/UIInputToolbarSample/Classes/BHInputToolbarViewController.h @@ -26,11 +26,9 @@ #import #import "BHInputToolbar.h" -@interface BHInputToolbarViewController : UIViewController { - BHInputToolbar *inputToolbar; +@interface BHInputToolbarViewController : UIViewController +{ - @private - BOOL keyboardIsVisible; } @property (nonatomic, strong) BHInputToolbar *inputToolbar; diff --git a/UIInputToolbarSample/Classes/BHInputToolbarViewController.m b/UIInputToolbarSample/Classes/BHInputToolbarViewController.m index 58753ec..44495ab 100644 --- a/UIInputToolbarSample/Classes/BHInputToolbarViewController.m +++ b/UIInputToolbarSample/Classes/BHInputToolbarViewController.m @@ -36,109 +36,57 @@ @implementation BHInputToolbarViewController #pragma mark - View lifecycle -- (void)loadView +- (void)viewDidLoad { - [super loadView]; - - keyboardIsVisible = NO; + [super viewDidLoad]; /* Calculate screen size */ - CGRect screenFrame = [[UIScreen mainScreen] applicationFrame]; - self.view = [[UIView alloc] initWithFrame:screenFrame]; - self.view.backgroundColor = [UIColor whiteColor]; + self.view.backgroundColor = [UIColor clearColor]; /* Create toolbar */ - self.inputToolbar = [[BHInputToolbar alloc] initWithFrame:CGRectMake(0, screenFrame.size.height-kDefaultToolbarHeight, screenFrame.size.width, kDefaultToolbarHeight)]; + self.inputToolbar = [[BHInputToolbar alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - kDefaultToolbarHeight, [UIScreen mainScreen].bounds.size.width, kDefaultToolbarHeight)]; [self.view addSubview:self.inputToolbar]; inputToolbar.inputDelegate = self; inputToolbar.textView.placeholder = @"Placeholder"; } -- (void)viewDidUnload -{ - [super viewDidUnload]; -} - - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; /* Listen for keyboard */ - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; /* No longer listen for keyboard */ - [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil]; } -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation +- (void)keyboardWillChange:(NSNotification *)noti { - return YES; -} - --(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration -{ - CGRect screenFrame = [[UIScreen mainScreen] applicationFrame]; - CGRect r = self.inputToolbar.frame; - if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) - { - r.origin.y = screenFrame.size.height - self.inputToolbar.frame.size.height - kStatusBarHeight; - if (keyboardIsVisible) { - r.origin.y -= kKeyboardHeightPortrait; - } - [self.inputToolbar.textView setMaximumNumberOfLines:13]; - } - else - { - r.origin.y = screenFrame.size.width - self.inputToolbar.frame.size.height - kStatusBarHeight; - if (keyboardIsVisible) { - r.origin.y -= kKeyboardHeightLandscape; + CGRect keyboardEndFrame = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue]; + CGFloat duration = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue]; + [UIView animateWithDuration:duration animations:^{ + if (keyboardEndFrame.origin.y == [UIScreen mainScreen].bounds.size.height) { + CGRect fm = inputToolbar.frame; + fm.origin.y = [UIScreen mainScreen].bounds.size.height - fm.size.height; + inputToolbar.frame = fm; + } else { + CGRect fm = inputToolbar.frame; + fm.origin.y = -(keyboardEndFrame.size.height + inputToolbar.frame.size.height - self.view.frame.size.height); + inputToolbar.frame = fm; } - [self.inputToolbar.textView setMaximumNumberOfLines:7]; - [self.inputToolbar.textView sizeToFit]; - } - self.inputToolbar.frame = r; + }]; } -#pragma mark - -#pragma mark Notifications - -- (void)keyboardWillShow:(NSNotification *)notification +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { - /* Move the toolbar to above the keyboard */ - [UIView beginAnimations:nil context:NULL]; - [UIView setAnimationDuration:0.3]; - CGRect frame = self.inputToolbar.frame; - if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { - frame.origin.y = self.view.frame.size.height - frame.size.height - kKeyboardHeightPortrait; - } - else { - frame.origin.y = self.view.frame.size.width - frame.size.height - kKeyboardHeightLandscape - kStatusBarHeight; - } - self.inputToolbar.frame = frame; - [UIView commitAnimations]; - keyboardIsVisible = YES; + return YES; } -- (void)keyboardWillHide:(NSNotification *)notification -{ - /* Move the toolbar back to bottom of the screen */ - [UIView beginAnimations:nil context:NULL]; - [UIView setAnimationDuration:0.3]; - CGRect frame = self.inputToolbar.frame; - if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { - frame.origin.y = self.view.frame.size.height - frame.size.height; - } - else { - frame.origin.y = self.view.frame.size.width - frame.size.height; - } - self.inputToolbar.frame = frame; - [UIView commitAnimations]; - keyboardIsVisible = NO; -} +#pragma mark - +#pragma mark Notifications -(void)inputButtonPressed:(NSString *)inputText {