-1

I'm trying to get my view to move up when a certain textField or textview is selected instead of every textfield i have. The view has 4 textfields and one textview. I want ONLY the 4th textfield and the textview to move up when the keyboard is being activated. (since the keyboard covers the textfield and textview) I've tried many suggestions given here, however none of them work for me.

I found an example (https://stackoverflow.com/a/21257034/5292561) of how i want it to look like, however when clicking on every textfield the view moves up rather than when i click only the 4th textfield or textview.

NOTE: I want to AVOID using scrollview since i don't like how it's being used. I've read negative things about it.

I'm still trying to learn so please bear with me. Thanks in advance

Here is the code for the textField and Textview i want to move up when clicking on the textfield and textview:

//dienstPicker input and styling

self.dienstPicker = [[UIPickerView alloc] init];
self.dienstPicker.delegate = self;
self.dienstPicker.dataSource = self;
self.dienstPicker.showsSelectionIndicator = YES;

[self.dienstPicker selectRow:1 inComponent:0 animated:YES];
//UITextField *textfield = [UIResponder currentFirstResponder];
[self.dienstTextField setInputView:_dienstPicker];
UIToolbar *toolBar2=[[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
[toolBar2 setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn2=[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                           style:UIBarButtonItemStylePlain
                                                          target:self
                                                          action:@selector(ShowSelectedOption2)];

UIBarButtonItem *space2= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                       target:nil
                                                                       action:nil];
[toolBar2 setItems:[NSArray arrayWithObjects:space2, doneBtn2, nil]];
[self.dienstTextField setInputAccessoryView:toolBar2];

self.dienstArray = @[@"test1",@"test2"];

    self.omschrijvingTextView.delegate = self;
    self.omschrijvingTextView.layer.borderWidth = 1.0f;
    self.omschrijvingTextView.layer.borderColor = [[UIColor lightGrayColor] CGColor];

UIToolbar *toolBar4=[[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
[toolBar4 setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn4=[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                           style:UIBarButtonItemStylePlain
                                                          target:self
                                                          action:@selector(ShowDescription)];
UIBarButtonItem *space4= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                       target:nil
                                                                       action:nil];
[toolBar4 setItems:[NSArray arrayWithObjects:space4, doneBtn4, nil]];
[self.omschrijvingTextView setInputAccessoryView:toolBar4];

CGRect textViewFrame = CGRectMake(0, 0, 320, 44);
UITextView *omschrijvingTextView = [[UITextView alloc] initWithFrame:textViewFrame];
omschrijvingTextView.returnKeyType = UIReturnKeyDone;

pragma mark - UIPickerViewDataSource

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if ([pickerView isEqual: _dienstPicker]){
        return [self.dienstArray count];
    } else if ([pickerView isEqual: _projectPicker]){
        return [self.projectArray count];
    } else return 0;
}

pragma mark - UIPickerViewDelegate

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if ([pickerView isEqual: _dienstPicker]){
        return self.dienstArray[row];
    } else if ([pickerView isEqual: _projectPicker]){
        return self.projectArray[row];
    } else return 0;
}

-(void)pickerView:(UIPickerView *)pickerView selectRow:(NSInteger)row inComponent:(NSInteger)component {
    if ([pickerView isEqual: _dienstPicker]){
        self.dienstTextField.text = self.dienstArray[row];
    } else if ([pickerView isEqual: _projectPicker]){
        self.projectTextField.text = self.projectArray[row];
    }
}
Community
  • 1
  • 1
Anna_JB
  • 11
  • 1
  • 10
  • Possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – Tj3n Nov 24 '15 at 11:11
  • You have to check if the textfield is the 4th textfield first, then only call the method that push the view up, in those guide they didn't check anything at all, U can get current textfield by calling `UITextField *textfield = [UIResponder currentFirstResponder];` – Tj3n Nov 24 '15 at 11:14
  • @Tj3n, i don't want the scrollview since i read a couple of times that it doesn't work nice. I've seen that post and it is not what i'm looking for. – Anna_JB Nov 24 '15 at 11:24
  • Apply my below answer in your coding.If it works, please tick my answer.Because it is helpful for other viewers when they see your question. – user3182143 Nov 24 '15 at 11:26

4 Answers4

2

in your Viewcontroller.h

@interface ViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate>
{
 CGFloat animatedDistance;

 }

in ViewController.M

paste the below 4 line in anywhere on the class (outside the method)

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;

in delegate methods are

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
  if(textField.tag==3) //textFieldFirst.tag =0,textFieldSecond.tag=1,textFieldThree.tag=2,textFieldFour.tag=3
  {
    CGRect textFieldRect =
    [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect =
    [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;
    if (heightFraction < 0.0)
    {
       heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
       heightFraction = 1.0;
    }


    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
 }
}

- (void)textFieldDidEndEditing:(UITextField *)textField

{
   if(textField.tag==3)
   {
     CGRect viewFrame = self.view.frame;
     viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
   }

 }

-(void)textViewDidBeginEditing:(UITextField *)textView
{
  CGRect textViewRect =
  [self.view.window convertRect:textView.bounds fromView:textView];
  CGRect viewRect =
  [self.view.window convertRect:self.view.bounds fromView:self.view];
  CGFloat midline = textViewRect.origin.y + 0.5 * textViewRect.size.height;
  CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
  CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
  CGFloat heightFraction = numerator / denominator;
  if (heightFraction < 0.0)
  {
    heightFraction = 0.0;
  }
  else if (heightFraction > 1.0)
  {
    heightFraction = 1.0;
  }
  animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
  CGRect viewFrame = self.view.frame;
  viewFrame.origin.y -= animatedDistance;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
  [self.view setFrame:viewFrame];
  [UIView commitAnimations];
}

- (void)textViewDidEndEditing:(UITextField *)textView
{
  CGRect viewFrame = self.view.frame;
  viewFrame.origin.y += animatedDistance;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
  [self.view setFrame:viewFrame];
  [UIView commitAnimations];
}
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

If you have implemented your textfields in scroll view and move them up like apple suggets when managing content with keyboard. You can assign tag to the text fields and execute the scroll movement when specific textfield is clicked by checking the tag of the text field. Hope this helps

  • What i've read about scrollview i don't like it. I was hoping there is a way around it. If i was to implement scrollview, would i have to add it on the whole view or only to the textfield and textview i want to scroll? – Anna_JB Nov 24 '15 at 11:03
  • You don't want to move your first 3,2 textfield to move up even if the fourth textfield is clicked. You have seen different app where whole of the textfields move up. If you dot want to use scroll vie than the same checking of tag on textfields work like, if(textField.tag == yourdesiredtag){ do animations; } – Muhammad Zohaib Ehsan Nov 24 '15 at 11:10
  • the 1st 2nd and 3rd can move up when clicking on the 4th, but i don't want the 1st 2nd and 3rd moving when i click on them and de keyboard appears. – Anna_JB Nov 24 '15 at 11:21
  • thats what i am saying you assign tag to that fourth text field and check in the editing begin method and if the textfield is fourth animate else do not. – Muhammad Zohaib Ehsan Nov 24 '15 at 11:23
0

There are many approaches to achieve that. One simple approach can be assign tags to each textField and than in delegate method

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
   Int *tagId = textView.tag;
if (tagId == "TAG OF YOUR TEXTFIELD") {
    // Move View up
    return YES;
 }
  return NO;
}
//Also Move your view down in textViewShouldEndEditing method.
Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
0
  1. If you are using storyboard, put all your textfields in a ScrollView and use this code on your ViewController.m
  2. Don't forget to set the textfields delegates to the ViewController (via storyboard or via code on ViewDidLoad

-(void)textFieldDidBeginEditing:(UITextField *)textFieldView {
if (textFieldView == self.myTextField)
    {
        CGPoint scrollPoint = CGPointMake(0.0, self.myTextField.frame.origin.y - self.myTextField.frame.size.height/2);
        [myScrollView setContentOffset:scrollPoint animated:YES];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    if (textField == self.myTextField)
    {
         CGPoint scrollPoint;
         [_email resignFirstResponder];
         scrollPoint = CGPointZero;
         [myScrollView setContentOffset:scrollPoint animated:YES];
    }
    return YES;
}
Rishil Patel
  • 1,977
  • 3
  • 14
  • 30