I have Webview with a toolbar below. When Keyboard appears I want to show Toolbar on top of Keyboard. How to achieve this?
Asked
Active
Viewed 533 times
0
-
Depends on how you are defining your layout. If you're using auto-layout, then I guess you have a constraint sticking the bottom of your tool bar to the bottom of your main view. Then you can change this constraint's constant when the keyboard shows up. If you are using pure frames, you can change the y origin of your tool bar when the keyboard shows up. To detect when the keyboard shows up / goes down, please see : http://stackoverflow.com/a/4374515/3844377 – Randy Sep 01 '16 at 12:47
-
I am using Autolayout. Can you guide on how to change constraint when Keyboard appears? – Signcodeindie Sep 01 '16 at 13:02
-
1Possible duplicate of [How to add ToolBar Above Keyboard?](http://stackoverflow.com/questions/23904848/how-to-add-toolbar-above-keyboard) – Pavel Gatilov Sep 01 '16 at 13:13
-
On UIWebview inputAccessoryView is readonly. – Signcodeindie Sep 01 '16 at 13:24
2 Answers
1
The following is a basic schema to achieve what you want:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(keyboardDidShowNotification:)
name: UIKeyboardDidShowNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(keyboardDidHideNotification:)
name: UIKeyboardDidHideNotification
object:nil];
}
-(void) keyboardDidShowNotification: (NSNotification * ) notification {
NSDictionary *dict = [notification userInfo];
CGRect keyboardFrame = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.view.frame = CGRectMake(0,0,
self.view.frame.size.width,
keyboardFrame.origin.y);
}
-(void) keyboardDidHideNotification: (NSNotification * ) notification {
self.view.frame = [UIApplication sharedApplication].keyWindow.frame;
}
-(void) dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
Hope it helps.

frankfg
- 625
- 5
- 13
0
I don't have a code sample but in words I can give a hint how to do it.
You need to create a NSLayoutConstraint
for the height between toolbar and the bottom part of your screen (or wherever it's placed).
Then after keyboard appears you need to count the height of the keyboard and adjust the layout constraint of the toolbar's height so that it will be above the keyboard. And then change it back when the keyboard is closed.
Hope it helps!

Tung Fam
- 7,899
- 4
- 56
- 63