I want to display a search bar above the keyboard. Whenever the keyboard appears search bar should also come. How to do this?
You can simply use textField.inputAccessoryView = your search bar
textField.inputAccessoryView = mySearchBar;
Create an object of UISearch bar and add an observer for keyboard in textFieldDidBeginEditing/textViewDidBeginEditing
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
int height = MIN(keyboardSize.height,keyboardSize.width);
int width = MAX(keyboardSize.height,keyboardSize.width);
//set frame for searchbar
searchBar.frame = CGRectMake(0, self.view.frame.size.height - height, width, 50);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -keyboardSize.height;
self.view.frame = f;
}];
}
-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}
Related
I have very strange issue which happens when keyboard appears they should shift textfield. It's work nice until I open and close Control Center (Video link)
In video I open and close Control Center fast and immediately press to textField, but if open and close Control Center and await around one second, it will work correctly.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardShow:) name: UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillResignActiveSelector:) name:#"applicationWillResignActive" object:nil];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void) applicationWillResignActiveSelector:(id)object {
[self dismissKeyboard];
}
- (void)dismissKeyboard {
if (self.textInput.isFirstResponder) {
[self.textInput resignFirstResponder];
}
}
- (void)keyboardShow:(NSNotification *)notification {
CGRect keyboard = [[info valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval duration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
self.view.center = CGPointMake(self.centerView.x, self.centerView.y - heightKeyboard);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, heightKeyboard - self.view.safeAreaInsets.top, 0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.bounds.size.width - 10);
[self.view layoutIfNeeded];
}
- (void)keyboardHide:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
NSTimeInterval duration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
self.tableView.delegate = nil;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.view.center = self.centerView;
self.chatBackgroundTopConstraint.constant = 0;
[self.view layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, self.tableView.contentInset.left, self.tableView.contentInset.right, self.tableView.bounds.size.width - 10);
} completion:^(BOOL finished) {
self.tableView.delegate = self;
}];
[self.tableView reloadData];
}
I have a UIView with UITextField that brings up a keyboard. i used UIKeyboardWillShowNotification/UIKeyboardWillHideNotification in Delegate method of textfield. as shown below:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
self.txtFullName.text=#"";
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
and my keyboard notification method here:
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize;
CGPoint keyboardOrigin;
CGFloat _currentKeyboardHeight = 0.0f;
NSDictionary* keyboardInfo = [notification userInfo];
keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
keyboardOrigin = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].origin;
CGFloat deltaHeight = keyboardSize.height - _currentKeyboardHeight;
_currentKeyboardHeight = keyboardSize.height;
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
CGFloat statusBarHeight = statusBarSize.height;
self.viewForSignature.frame = CGRectMake(self.viewForSignature.frame.origin.x, self.viewForSignature.frame.origin.y+statusBarHeight+self.navigationController.navigationBar.frame.size.height-deltaHeight, self.viewForSignature.frame.size.width, self.viewForSignature.frame.size.height);
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
-(void)keyboardWillHide:(NSNotification *)notification
{
CGSize keyboardSize;
CGFloat _currentKeyboardHeight = 0.0f;
NSDictionary* keyboardInfo = [notification userInfo];
keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat deltaHeight = keyboardSize.height - _currentKeyboardHeight;
_currentKeyboardHeight = keyboardSize.height;
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
CGFloat statusBarHeight = statusBarSize.height;
self.viewForSignature.frame = CGRectMake(self.viewForSignature.frame.origin.x,self.viewForSignature.frame.origin.y-statusBarHeight-self.navigationController.navigationBar.frame.size.height+deltaHeight, self.viewForSignature.frame.size.width, self.viewForSignature.frame.size.height);
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
screenshot of demo given below:
move up and down keyboard when click on return key, works perfectly. but issue when click on textfield and sudden tap on return key the view up. as given in below scrrenshot:
what should i do? Thanks in advance
I want to raise my keyboard when i press on a determinate UITextField, so I create a touch down action, but at the first call it doesn't work, by second call it work fine.
here's the code (naturally keyboardSize is a global variable with CGSize type):
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
}
- (void)keyboardWillHide:(NSNotification *)notification {
[self moveFrameToVerticalPosition:0.0f forDuration:0.3f];
}
- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration {
CGRect frame = self.view.frame;
frame.origin.y = position;
[UIView animateWithDuration:duration animations:^{
self.view.frame = frame;
}];
}
- (IBAction)insert:(id)sender {
float newVerticalPosition = - keyboardSize.height;
[self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.0f];
}
In keyboardWillShow: add:
[self moveFrameToVerticalPosition:-keyboardSize.height forDuration:0.0f];
You don't really need insert:.
On my iPhone app i have a UIWebView with toolbars above an below it. the toolbar on the bottom contains a text box for the user to input some text. but when i click on the text box, the keyboard covers the bottom half of the screen.
how do i make it so that the toolbars stay above and below the webview but the the height of the webview shrinks for the keyboard to be displayed?
any guidance on this is appreciated.
thanks in advanced.
To shrink webView you need the following:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
NSTimeInterval keyboardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions keyboardAnimationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
CGFloat keyboardHeight = keyboardFrame.size.height;
[UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationCurve
animations:^{
_webView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
}
completion:NULL];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSTimeInterval keyboardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions keyboardAnimationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
[UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationCurve
animations:^{
_webView.contentInset = UIEdgeInsetsZero;
}
completion:NULL];
}
In the case of additional subviews you should slightly change this code (add change of the frames for other subview)
You can implement UITextFieldDelegate in your UIViewController and set the UITextField delegate value to your controller and then implement textFieldDidBeginEditing and textFieldDidEndEditing methods in your controller to detect when editing starts/ends.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
// in case you have more than one text fields in the same view
if(textField == self.YOUR_FIELD_NAME)
// change the web view height here, you can also animate it using UIView beginAnimations
CGRect frame = self.webView.frame;
frame.size.height = 200;
self.webView.frame = frame;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
// do the opposite here
}
How do I do it whatsapp style, where the message space, when clicked, would push a keyboard up from the bottom, as well as pushing the toolbar up as well. And then when cancelled (i.e. clicking in the background), it'll push the keyboard back down with the toolbar
This is done with the UIKeyboardWillShowNotification, UIKeyboardDidShowNotification, UIKeyboardWillHideNotification and UIKeyboardDidHideNotification notifications.
Then when you handle the notification you adjust the height of the frame:
For example:
- (void) keyboardWillShow:(NSNotification *)aNotification{
NSDictionary* info = [aNotification userInfo];
NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGFloat keyboardHeight = [aValue CGRectValue].size.height;
self.keyboardVissible = YES;
[UIView animateWithDuration:duration animations:^{
CGRect frame = self.contentView.frame;
frame.size.height -= keyboardHeight;
self.contentView.frame = frame;
}];
}
You will need to register to receive the notification, you should only listen to the keyboard notification when the view is visible, strange thing could happen if you do it in the viewDidLoad:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}