Am using the below code to get keyboard height which varies in IPhone 5s device with ios8 compared to IPhone4s device with ios7 .As a result my textfield is moving very high when i tap on it in IPhone5s with ios8 while the same code works fine in IPhone 4s with ios7.Can someone guide how the problem can be fixed in both versions.
- (void)keyboardWasShown:(NSNotification*)notification
{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float kbHeight;
if (([UIApplication sharedApplication].statusBarOrientation== UIDeviceOrientationPortraitUpsideDown)||([UIApplication sharedApplication].statusBarOrientation== UIDeviceOrientationPortrait))
{
kbHeight=kbSize.height;
}
else
{
kbHeight=kbSize.width;
}
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbHeight, 0.0);
self.scroll.contentInset = contentInsets;
self.scroll.scrollIndicatorInsets = contentInsets;
CGRect rect = self.view.frame;
rect.size.height -= (kbHeight);
if (!CGRectContainsPoint(rect, self.activeField.frame.origin))
{
CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y - ((kbHeight) - self.activeField.frame.size.height));
[self.scroll setContentOffset:scrollPoint animated:NO];
}
}
Simply Replace the line from your code
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
With
CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
Let me know if it works
This is not an issue, you are getting different size keyboard because Predictive is Enable.
The height of keyboard is 216 which is fixed but when Predictive is Enabled you will get 253 as height.
So you have to write code for both conditions.
use this code, may help you
- (void)keyboardWillShow:(NSNotification*)note {
NSDictionary* info = [note userInfo];
CGSize _kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float kbHeight = _kbSize.width > _kbSize.height ? _kbSize.height : _kbSize.width;
}
kbHeight variable stored height of the keyboard.
Related
I have a scroll view with many text fields as subviews, I wanted the selected text field(_activeField) to scroll up when keyboard appears.
- (void) keyboardUP:(NSNotification *)notification
{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height + 10, 0.0);
_scroller.contentInset = contentInsets;
_scroller.scrollIndicatorInsets = contentInsets;
CGRect aRect = App_Delegate.window.frame;
aRect.size.height -= kbSize.height;
CGRect rect = [_activeField convertRect:_activeField.frame fromView:App_Delegate.window];
if (!CGRectContainsPoint(aRect, rect.origin) )
{
[_scroller scrollRectToVisible:rect animated:YES];
}
}
This code is working fine for iOS 10, but in iOS 11 , its not working ,"scrollRectToVisible" doesn't scroll the ScrollView even if its content size is correct.
I am not sure about
- (void) keyboardUP:(NSNotification *)notification
but have seen a similar problem with
- (void)handleKeyobardDidShowNotification:(NSNotification *)notification
It looks like this no longer runs on the main thread in iOS 11.
Try running you call to scrollRectToVisible on the main thread as follows
dispatch_async(dispatch_get_main_queue(), ^{
[_scroller scrollRectToVisible:targetPosition animated:YES];
});
This is a duplicate of this issue.
use the UIKeyboardFrameEndUserInfoKey
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
print(keyboardSize)
}
}
and for ObjC
- (void)keyboardWillShow:(NSNotification *)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
}
So I'm not sure why my code isn't working. When the keyboard appears it puts the toolBar above the keyboard, but much higher than the height of the keyboard. Also the quick text bar in ios8 messes it up even more when I toggle it on and off, and the toolBar doesn't adjust correctly. Also when I close out the keyboard the first time the toolBar goes back to its original position, but then after once it doesn't go back down all the way where it was originally. I logged out self.yPositionStore and it never changes, which is why I don't understand why it doesn't always go back to the same spot. I've literally been working on this code all day trying to find a solution to my textfield getting hidden by the keyboard and it's giving me a headache. someone PLEASE help me.
- (void)viewDidLoad {
[super viewDidLoad];
self.yPositionStore = self.toolBar.frame.origin.y;
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect newFrame = self.toolBar.frame;
newFrame.origin.y = kbSize.height;
self.toolBar.frame = newFrame;
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
CGRect newFrame = self.toolBar.frame;
newFrame.origin.y = self.yPositionStore;
self.toolBar.frame = newFrame;
}
There was problem in keyboardwasshown method. you have to subtract toolbar height and keyboard height from main screen height to find exact y value for toolbar.
- (void)viewDidLoad {
[super viewDidLoad];
self.yPositionStore = self.toolBar.frame.origin.y;
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect newFrame = self.toolBar.frame;
newFrame.origin.y = [UIScreen mainscreen].bounds.size.height - kbSize.height - newFrame.size.height;
self.toolBar.frame = newFrame;
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
CGRect newFrame = self.toolBar.frame;
newFrame.origin.y = self.yPositionStore;
self.toolBar.frame = newFrame;
}
ok so for the most part this winded up working
- (void)keyboardWasShown:(NSNotification*)aNotification
{
self.yPositionStore = self.toolBar.frame.origin.y;
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect newFrame = self.toolBar.frame;
newFrame.origin.y = [UIScreen mainScreen].bounds.size.height - kbSize.height - newFrame.size.height;
self.toolBar.frame = newFrame;
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
CGRect newFrame = self.toolBar.frame;
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
newFrame.origin.y += kbSize.height - newFrame.size.height;
self.toolBar.frame = newFrame;
}
After the first time the keyboard is hidden, the tool bar goes down about a pixel further than it's suppose to, but it still looks good. The other problem I'm still having is on keyboardWasShown, the toolbar is repositioning itself but it's a little bit delayed, and I'm not sure how to fix that. Now off to trying out how to reposition a tableview with the keyboard!
I have textViews in a tableView. When textViews (towards the bottom of the tableView) get selected the keyboard pops up, and hides the textView, and you can't scroll down either. So I added the following code:
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
myTableView.contentInset = contentInsets;
myTableView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.myTabelView scrollRectToVisible:activeField.frame animated:YES];
}
}
I now have the opposite problem. When the keyboard does show, towrad the top doesn't show, and it's not possible to scroll all the way up either.
Hi and did you try to use TPKeyboardAvoiding ? I use it to solve such cases without to need to code anything... You can try it as CocoaPod quickly.
I want to handle keyboard notifications to move textfield up and down on tap of it in portrait as well as landscape ..how to get the height of keyboard in portrait and landscape to achieve this?
use this code
- (void)keyboardWillShow:(NSNotification*)note {
NSDictionary* info = [note userInfo];
CGSize _kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float kbHeight = _kbSize.width > _kbSize.height ? _kbSize.height : _kbSize.width;
}
it will work in all the cases.
Let me know if it works.
You can find Height of keyboard using following code.
// Works in both portrait and landscape mode
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect toView:nil];
CGSize kbSize = kbRect.size;
NSLog(#"Keyboard Height: %f Width: %f", kbSize.height, kbSize.width);
I have a custom inherited UIView class with a UITableView within it as its only subview. I'm trying to mimic the normal functionality of the UITableViewController when the keyboard is shown to adjust the contentInset and scrollIndicatorInsets of the table view to the height of the keyboard. This is my method that gets called when the keyboard did show from within my custom UIView class:
- (void)keyboardDidShow:(NSNotification*)notification
{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
_tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_tableView.scrollIndicatorInsets = _tableView.contentInset;
}
This works to a certain extent, but there is still some overlap of the keyboard onto the table view for some reason by maybe ten or so pixels.
I'm thinking it has something to do with not taking into account some of the other screen geometry but I don't see how that could be. The height of the keyboard should be exactly what I need because the tableView stretches all the way to the bottom of the screen. Any ideas?
Change the tableView.frame.size.height, to account for the keyboard.
when keyboard is showing, reduce the height,
when not showing, increase the height.
refer to this if you want to consider the keyboard height for all possibilities http://www.idev101.com/code/User_Interface/sizes.html
Dont mess with the contentInset and the scrollIndicatorInsets. Just setting the frameSize will take care of these for you.
this is how your method should be
- (void)keyboardDidShow:(NSNotification*)notification
{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect rect = _tableView.frame;
rect.size.height = _tableView.frame.size.height - kbSize.height;
_tableView.frame = rect;
}
- (void)keyboardWillHide:(NSNotification*)notification
{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect rect = _tableView.frame;
rect.size.height = _tableView.frame.size.height + kbSize.height;
_tableView.frame = rect;
}
I have used this piece of code for a similar functionality. So if its still not working, there's something else going wrong.
I am curious why this isn't working for you, as I have basically the same thing and it is working for me. There is only one difference that I can see, in that I don't access '_tableView' and instead make sure that I'm always using the getter and setter.
Here is what I do, that is working.
- (void)keyboardDidShow:(NSNotification *)keyboardNotification
{
NSDictionary *info = [keyboardNotification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGFloat newBottomInset = 0.0;
UIEdgeInsets contentInsets;
if (UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ) {
newBottomInset = keyboardSize.height;
} else {
newBottomInset = keyboardSize.width;
}
contentInsets = UIEdgeInsetsMake(0.0, 0.0, newBottomInset, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}
Note that my app allows device rotation and when that happens the value used needs to be the width of the keyboard because the values are relative to the portrait orientation, which caused me hours of confusion.
Hopefully the self.tableView access will make the difference.