2

I have a view as follow:

enter image description here

In my code I want to move this view keyboard size:

-(void) keyboardWillShow:(NSNotification *) notification {
    NSDictionary* keyboardInfo = [notification userInfo];

    // Work out where the keyboard will be
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

    // Work out animation duration
    NSTimeInterval animationDuration =[[keyboardInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationOptions keyboardAnimationCurve = [[keyboardInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];


    // Animate this
    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:keyboardAnimationCurve
                     animations:^(){
                         self.view.frame = CGRectOffset(self.view.frame, 0, -keyboardFrameBeginRect.size.height);
                     }
                     completion:NULL];
}

It is moving everything to top except child views. Then I tried to move child views to the top as follow:

-(void) keyboardWillShow:(NSNotification *) notification {
    NSDictionary* keyboardInfo = [notification userInfo];

    // Work out where the keyboard will be
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

    // Work out animation duration
    NSTimeInterval animationDuration =[[keyboardInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationOptions keyboardAnimationCurve = [[keyboardInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];


    // Animate this
    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:keyboardAnimationCurve
                     animations:^(){
                         self.view.frame = CGRectOffset(self.view.frame, 0, -keyboardFrameBeginRect.size.height);
                         self.memberNumberLayout.frame = CGRectOffset(self.memberNumberLayout.frame, 0, -keyboardFrameBeginRect.size.height);
                     }
                     completion:NULL];
}

self.memberNumberLayout is a subview of main UIView. But it is not moving. What could force sub view to stay there? Could it be because of constrains? As you can see to subviews and one label has not move with their parent view!

enter image description here

Constrains

enter image description here

enter image description here

Bernard
  • 3,880
  • 18
  • 49
  • 82
  • 1
    hi @Bernard, can you post a screenshot of your constraints layouts ? – Basheer_CAD Apr 04 '16 at 01:39
  • @Basheer_CAD Yes mate. I added constrains as well. – Bernard Apr 04 '16 at 01:45
  • 1
    Possible duplicate of [Why moving UIView is not moving UIImageView on same view?](http://stackoverflow.com/questions/36392780/why-moving-uiview-is-not-moving-uiimageview-on-same-view) – Avi Apr 04 '16 at 03:49

2 Answers2

1

The problem is that you are saying self.view.frame =. You can't do that — this view is fixed as a subview of the window. Give self.view a content view the same size at itself (pinned to all four sides), with all other views inside it, and when the time comes, move the content view. Moreover, you've got constraints on these views (auto layout), so you must not change the frame of the content view; to move it, change its constraints.

matt
  • 447,615
  • 74
  • 748
  • 977
  • Hey @matt Can you explain your answer a bit more. How can I give a content view the same size at itself? Do you mean I add another view under main view and add element on that view or you mean I do it programmatically? – Bernard Apr 04 '16 at 02:13
  • No programmatically, no. I just mean you plan ahead because you know you're going to be shifting things later. Downloadable example here: https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/iOS7bookExamples/bk2ch10p520textFieldSliding Old but might still work – matt Apr 04 '16 at 02:23
1

While it is really hard to tell the problem from your constraints hierarchy screenshots, I think the problem is related to top constraints for self.memberNumberLayout my suggestions are the following:

1- use constrains instead of frames to animate your views by using IBOutlet from the top constraints of the container view then use the constant e.g self.containerConstraint.constant = keyboardSize * -1;

2- use scrollviews for such problems. UIScrollView is a great view for moving views up when keyboard appears or disappears, also this is can be very helpful when you run your app on different devices. Refer to this question for more information: How to make a UITextField move up when keyboard is present?

Community
  • 1
  • 1
Basheer_CAD
  • 4,768
  • 22
  • 35