3

I am very junior mobile programmer .I need to move up text views when keyboard appears.I follows this move-uiview-up-when-the-keyboard-appears-in-ios and it works well but I have a background image and I do not want to move up background image .so all textboxes are embed in UIView named as customView.I tried to move up customView instead of self.view .When I start enter in first textview, the customView moves up.But when I move to second textview,customview moves down to original position and textView become under the keyboard.customView need to stay move up the when i start enter in second textview .I really appreciate any help!.

@property (strong, nonatomic) IBOutlet UIView *customView;
 -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES; }


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

[self.view endEditing:YES];
return YES; }


- (void)keyboardDidShow:(NSNotification *)notification
{
  //Assign new frame to your view 
    [self.customView setFrame:CGRectMake(0,50,320,460)]; 

}

-(void)keyboardDidHide:(NSNotification *)notification
{
    [self.customView setFrame:CGRectMake(0,193,320,460)];
}
Community
  • 1
  • 1
sasha
  • 343
  • 1
  • 6
  • 14

3 Answers3

7

Add the observer in viewDidLoad for best approach.

- (void)viewDidLoad {

    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

}

- (void)keyboardWillShow:(NSNotification*)aNotification {
    [UIView animateWithDuration:0.25 animations:^
     {
         CGRect newFrame = [customView frame];
         newFrame.origin.y -= 50; // tweak here to adjust the moving position 
         [customView setFrame:newFrame];

     }completion:^(BOOL finished)
     {

     }];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    [UIView animateWithDuration:0.25 animations:^
     {
         CGRect newFrame = [customView frame];
         newFrame.origin.y += 50; // tweak here to adjust the moving position
         [customView setFrame:newFrame];

     }completion:^(BOOL finished)
     {

     }];

    }

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
Ramesh Annadurai
  • 356
  • 1
  • 10
  • It works fine in ios7 but it doesn't work in ios8.I am not sure why.Could you please help me? – sasha Oct 31 '14 at 01:55
  • It works fine in iOS 8.1, not sure about 8.0, please check in 8.1. – Ramesh Annadurai Oct 31 '14 at 03:48
  • Thank you .I found solution for ios8 in http://stackoverflow.com/questions/26112319/ios8-whats-going-on-with-moving-views-during-keyboard-transitions. – sasha Oct 31 '14 at 04:33
  • What is the customview here? – Ravi Ojha Apr 20 '16 at 11:34
  • @RaviJSS The view that sasha wants to adjust the position based on visibility of soft keyboard. You can use self.view instead of customView if you don't have it. – Ramesh Annadurai Apr 21 '16 at 06:32
  • what is 'customView'? – Ignacio Oroná Mar 09 '17 at 16:47
  • @IgnacioOroná sasha have created a separate view (customView) with required items and added as a subview to self.view. If you don't have any subview inside the self.view you can directly set the frames to self.view; – Ramesh Annadurai Mar 22 '17 at 17:35
  • if i am having n number of text fields , lets guess i have signup page to fill up . so that time top text field should not move up , middle text field should move up little bit and last text field should move up to the mark that user can see it . so that time , what would be the code modification ? – Moxarth Jun 29 '17 at 13:01
1

// 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;
}
0xmtn
  • 2,625
  • 5
  • 27
  • 53
Teja Kumar Bethina
  • 3,486
  • 26
  • 34
0

Make your class implement the UITextFieldDelegate.

Put the following code in viewDidLoad.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

And define the following functions in your .m file.

- (void)keyboardWasShown:(NSNotification *)aNotification

{// scroll to the text view
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible.
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
self.scrollView.scrollEnabled = YES;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
// scroll back..
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
self.scrollView.scrollEnabled = NO;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
Akshay Bhalotia
  • 799
  • 4
  • 11