diff --git a/backends/platform/ios7/ios7_keyboard.h b/backends/platform/ios7/ios7_keyboard.h index af9e5be0be80..44bb65e79b5c 100644 --- a/backends/platform/ios7/ios7_keyboard.h +++ b/backends/platform/ios7/ios7_keyboard.h @@ -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; diff --git a/backends/platform/ios7/ios7_keyboard.mm b/backends/platform/ios7/ios7_keyboard.mm index 069cbd190057..9b3ca6698197 100644 --- a/backends/platform/ios7/ios7_keyboard.mm +++ b/backends/platform/ios7/ios7_keyboard.mm @@ -36,7 +36,9 @@ @interface TextInputHandler : UITextView { } - (id)initWithKeyboard:(SoftKeyboard *)keyboard; -- (void)updateToolbarSize; +- (void)dealloc; +- (void)attachAccessoryView; +- (void)detachAccessoryView; @end @@ -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; @@ -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:)]; @@ -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) { @@ -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]; }