Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions UIInputToolbarSample/Classes/BHInputToolbarViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
#import <UIKit/UIKit.h>
#import "BHInputToolbar.h"

@interface BHInputToolbarViewController : UIViewController <BHInputToolbarDelegate> {
BHInputToolbar *inputToolbar;
@interface BHInputToolbarViewController : UIViewController <BHInputToolbarDelegate>
{

@private
BOOL keyboardIsVisible;
}

@property (nonatomic, strong) BHInputToolbar *inputToolbar;
Expand Down
98 changes: 23 additions & 75 deletions UIInputToolbarSample/Classes/BHInputToolbarViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down