Keyboard overlap view (swift) - ios

I have a problem, when i put a text field in a simple view.
The keyboard overlaps the view, how can i make than the keyboard push the view?
I have to set an scrollview?

use this, hopping that ur textview in a scrollview and tag of it is 99
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
UIScrollView *MyscrollView = (UIScrollView *)[self.view viewWithTag:99];
[UIView animateWithDuration:1.0f animations:^(void){
[MyscrollView setContentOffset:CGPointMake(0, 40) animated:YES];
}];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:1.0f animations:^(void){
UIScrollView *MyscrollView = (UIScrollView *)[self.view viewWithTag:99];
[MyscrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}];
[textField resignFirstResponder];
}

Related

View doesn't move up when keyboard is present iOS

I am trying to use this code
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%f",self.view.frame.origin.y);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25f];
CGRect frame = self.view.frame;
frame.origin.y =frame.origin.y -204;
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25f];
CGRect frame = self.view.frame;
frame.origin.y = frame.origin.y + 204;
[self.view setFrame:frame];
[UIView commitAnimations];
}
To move view up when keyboard is present but this results like this:
becomes this:
Can't figure out why and if it has to do something with the navbar or constraints.
Why not to use this beautiful library named IQKeyboardManager. Just add it through pods/manually and let it do your work
Kind of what Alexey Sobolevsky said did the trick. Basically i needed to put them in a scrollview and add a constraint from the scroll view to the object beneath, then put all the views containing textfields inside that scrollview instead. This made everything work correctly.
First add the scroll view.
Then write the following code.
//When return key press then bring the view back
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
self.scrlView.contentOffset = CGPointMake(0, 0);
return YES;
}
//When the text field begin editing then moves the view up
-(void)textFieldDidBeginEditing:(UITextField *)textField{
self.scrlView.contentOffset = CGPointMake(0, 30);
}
//When ext field end editing then bring the view back
-(void)textFieldDidEndEditing:(UITextField *)textField{
self.scrlView.contentOffset = CGPointMake(0, 0);
}

How to move View up when Keyboard appeared in small screen like 4s , 5 and 5s but view should not move up when its enter inside iphone 6

How to move View up when Keyboard appeared in small screen like 4s, 5 and 5s but view should not move up when its enter inside iphone 6 and 6S.
I am sharing my code which is i have done in my app for all screen but in my condition view should not move up when its enter in 6 or 6s.
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
const int movementDistance = -60; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: #"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
I suggest you use IQKeyboardManager It will manage everything for you with less efforts
// Add a scrollview on main view and add UITextField on that scrollview
-(void) viewDidLoad
{
UIScrollView *myScrollView = [ [UIScrollView alloc] initWithFrame:[UIScreen mainScreen ].bounds];
[myScrollView .contentSize =CGSizeMake(320, 500);
[myScrollView .contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
[self.view addSubView:myScrollView ];
UITextField *myTextField = [ [UITextField alloc] initWithFrame:CGRectMake(20,30,100,33)];
[myScrollView addSubView:myTextField ];
myTextField.delegate = self;
}
// Set the scrollview content offset to make the myTextField move up
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[myScrollView setContentOffset:CGPointMake(0,textField.center.y-80) animated:YES];
// here '80' can be any number which decide the height that textfiled should move
}
//To move the textfield to its original position
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[[myScrollView setContentOffset:CGPointMake(0,0) animated:YES];
[textField resignFirstResponder];
return YES;
}

How to change UITableView size when UIKeyboard is visible?

I have an application with a UITableView that has UITextFields inside UITableViewCells which the user will have either the virtual UIKeyboard or a Bluetooth keyboard connected to enter text into the UITextFields.
If the Bluetooth keyboard is connected I would like to keep the UITableView full height when selecting and entering text. When there is no Bluetooth keyboard connected if its visible I would like to reduce the UITableview to fit with the UIkeyboard is showing, if its not showing then I would like to make the UITableView full size again.
I have tried to do this by using the UIKeyboard delegate methods
- (void)keyboardDidShow:(NSNotification *)aNotification;
- (void)keyboardDidHide:(NSNotification *)aNotification;
For some reason keyboardDidShow is not accessed ever but keyboardDidHide is accessed when the UIKeyboard is removed from view and I cannot figure out why.
- (void)keyboardDidShow:(NSNotification *)aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.tblView.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - 255); // or -216
[UIView commitAnimations];
}
- (void)keyboardDidHide:(NSNotification *)aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.2f];
self.tblView.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - height);
[UIView commitAnimations];
}
And in UITextField Delegate Methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:self.tblView];
CGPoint pt = rc.origin;
pt.x = 0;
if(rc.origin.y > 200)
pt.y -= 150;
else
pt.y -= rc.origin.y;
[self.tblView setContentOffset:pt animated:YES];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
use optimized way like this
selectRowAtIndexPath:animated:scrollPosition:
Selects a row in the receiver identified by index path, optionally scrolling the row to a location in the receiver.
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
Special Considerations
Passing UITableViewScrollPositionNone will result in no scrolling, rather than the minimum scrolling described for that constant. To scroll to the newly selected row with minimum scrolling, select the row using this method with UITableViewScrollPositionNone, then call scrollToRowAtIndexPath:atScrollPosition:animated: with UITableViewScrollPositionNone.
NSIndexPath *rowToSelect; // assume this exists and is set properly
UITableView *myTableView; // assume this exists
[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone];
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNone animated:YES];
more understand Setting scroll position in UITableView

View is not set back to original position

In my xib i have set one text box ...when user edit in it i am moving that view little up...but when i return back it to its original postion it not come to its original postion it remain little up then original postion.
Here is viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])self.edgesForExtendedLayout = UIRectEdgeNone;
search=FALSE;
UIButton *btnOther = [UIButton buttonWithType:UIButtonTypeSystem]; //UIButtonTypeCustom for image button
[btnOther setTitle:#"Save" forState:UIControlStateNormal];
btnOther.titleLabel.font = [UIFont fontWithName:GZFont size:12.0f];
// [btnSave setImage:[UIImage imageNamed:#"save.png"] forState:UIControlStateNormal];
[btnOther addTarget:self action:#selector(btnSaveAction:) forControlEvents:UIControlEventTouchUpInside];
[btnOther setFrame:CGRectMake(0, 0, 55, 29)];
dataArray=[[NSMutableArray alloc]init];
nameSearchArray=[[NSMutableArray alloc]init];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnOther];
txtCharityName.hidden=YES;
txtCharityMail.hidden=YES;
originalCenter=self.view.center;
}
here is textfield delegate methods.:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if(textField==txtGratuity){
self.view.center=CGPointMake(originalCenter.x, originalCenter.y-30);
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
self.view.center=CGPointMake(originalCenter.x, originalCenter.y);
charityId=#"";
NSLog(#"charityID%#",charityId);
}
and here is screenshot.:
as per this equation
frame.origin = center - (bounds.size / 2.0)
center = frame.origin + (bounds.size / 2.0)
you should add 30 points for textfield to get to correct position.
- (void)textFieldDidEndEditing:(UITextField *)textField {
self.view.center=CGPointMake(originalCenter.x, originalCenter.y+30);
charityId=#"";
NSLog(#"charityID%#",charityId);
}
Edit:
try this in your
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView animateWithDuration:0.25
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect frame;
// let's move our textField
frame = textField.frame;
frame.origin.y = frame.origin.y-30;
textField.frame=frame;
}
completion:^(BOOL finished){
if(finished) NSLog(#"Finished !!!!!);
}];
}
and move textfield down after edit
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView animateWithDuration:0.25
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect frame;
// let's move our textField
frame = textField.frame;
frame.origin.y = frame.origin.y+30;
textField.frame=frame;
}
completion:^(BOOL finished){
if(finished) NSLog(#"Finished !!!!!);
}];
}
Set the textfield frames in viewWillAppear instead of viewDidLoad method.

can't use Frame.origin while setText

I'm using the next code to fadeout and scroll up my textfield but I just noticed that when I use something like [textField setText:#"something"]; or [textField resignFirstResponder]; I lose the scrolling up. Any one care to explain why I'm getting this behavior and a posibble workaround.
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^ {
CGRect Frame = textField.frame;
Frame.origin.y=100;
textField.frame=Frame;
textField.alpha = 0.0;
}
completion:^(BOOL finished) {
textField.hidden = YES;
}];
[textField resignFirstResponder];
}
You should put the [textField resignFirstResponder]; into the completion block.

Resources