Skip to content

Commit

Permalink
IOS7: Hide input accessory toolbar when an external keyboard is conne…
Browse files Browse the repository at this point in the history
…cted
  • Loading branch information
criezy committed Jul 6, 2019
1 parent b37ac33 commit cfd6617
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions backends/platform/ios7/ios7_keyboard.h
Expand Up @@ -34,11 +34,13 @@
}

- (id)initWithFrame:(CGRect)frame;
- (void)dealloc;
- (UITextView *)inputView;
- (void)setInputDelegate:(id)delegate;
- (void)handleKeyPress:(unichar)c;
- (void)handleMainMenuKey;

- (void)prepareKeyboard:(NSNotification *)notification;
- (void)showKeyboard;
- (void)hideKeyboard;

Expand Down
47 changes: 43 additions & 4 deletions backends/platform/ios7/ios7_keyboard.mm
Expand Up @@ -36,7 +36,9 @@ @interface TextInputHandler : UITextView {
}

- (id)initWithKeyboard:(SoftKeyboard *)keyboard;
- (void)updateToolbarSize;
- (void)dealloc;
- (void)attachAccessoryView;
- (void)detachAccessoryView;

@end

Expand All @@ -54,7 +56,7 @@ - (id)initWithKeyboard:(SoftKeyboard *)keyboard {
//item.leadingBarButtonGroups = @[];
//item.trailingBarButtonGroups = @[];

toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)] autorelease];
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
toolbar.barTintColor = keyboard.backgroundColor;
toolbar.tintColor = keyboard.tintColor;
toolbar.translucent = NO;
Expand Down Expand Up @@ -106,13 +108,30 @@ - (id)initWithKeyboard:(SoftKeyboard *)keyboard {
return self;
}

- (void)updateToolbarSize {
-(void)dealloc {
[toolbar release];
[scrollView release];
[super dealloc];
}

- (void)attachAccessoryView {
self.inputAccessoryView.hidden = NO;
// Alternatively we could add/remove instead of show/hide the inpute accessory view
// self.inputAccessoryView = scrollView;
// [self reloadInputViews];
// We need at least a width of 768 pt for the toolbar. If we add more buttons this may need to be increased.
toolbar.frame = CGRectMake(0, 0, MAX(768, [[UIScreen mainScreen] bounds].size.width), toolbar.frame.size.height);
toolbar.bounds = toolbar.frame;
scrollView.contentSize = toolbar.frame.size;
}

- (void)detachAccessoryView {
self.inputAccessoryView.hidden = YES;
// Alternatively we could add/remove instead of show/hide the inpute accessory view
// self.inputAccessoryView = nil;
// [self reloadInputViews];
}

- (NSArray *)keyCommands {
UIKeyCommand *upArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputUpArrow modifierFlags: 0 action: @selector(upArrow:)];
UIKeyCommand *downArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputDownArrow modifierFlags: 0 action: @selector(downArrow:)];
Expand Down Expand Up @@ -227,9 +246,15 @@ - (id)initWithFrame:(CGRect)frame {
inputDelegate = nil;
inputView = [[TextInputHandler alloc] initWithKeyboard:self];
inputView.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(prepareKeyboard:) name:UIKeyboardWillShowNotification object:nil];
return self;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
unichar c;
if (text.length) {
Expand Down Expand Up @@ -258,8 +283,22 @@ - (void)handleMainMenuKey {
[inputDelegate handleMainMenuKey];
}

- (void)prepareKeyboard:(NSNotification *)notification {
// Check if a hardware keyboard is connected, and only show the accessory view if there isn't one.
// If there is a hardware keyboard, the software one will only contains the text assistance bar
// and will be small (less than 100 pt, but use a bit more in case it changes with future iOS versions).
// This only works with iOS 9 and above. For ealier version the keyboard size is fixed and instead it
// only shows part of the keyboard (which could be detected with the origin position, but that would
// depend on the current orientation and screen resolution).
NSDictionary* info = [notification userInfo];
CGRect keyboardEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
if (keyboardEndFrame.size.height < 140)
[inputView detachAccessoryView];
else
[inputView attachAccessoryView];
}

- (void)showKeyboard {
[inputView updateToolbarSize];
[inputView becomeFirstResponder];
}

Expand Down

0 comments on commit cfd6617

Please sign in to comment.