0

I'm trying to animate all views up when showing the keyboard to avoid overlap.

However, this old trick no longer seems to work:

 for (UIView *subview in self.view.subviews){
     CGRect r = subview.frame;
     r.origin.y += yOffSet;
     [UIView animateWithDuration:duration animations:^{subview.frame = r;} completion:block];
 }

Any idea why?

==== UPDATE

These subviews don't animate on my iPhone6 running iOS8. They do however on my iPhone4 running iOS 7.1.2. Both use AutoLayout.

Snowcrash
  • 80,579
  • 89
  • 266
  • 376
  • Probably because what you want to move at the top is not in self.view hierarchy. – Andrea Feb 02 '15 at 17:52
  • 2
    Are you using Auto Layout? That could affect the frame animations. – TotoroTotoro Feb 02 '15 at 17:53
  • This code is working fine on my iPhone 4 running iOS7.1.2. – Snowcrash Feb 02 '15 at 17:57
  • Can you describe what doesn't work about it? When I run the code you have posted, I see my views animate a change in position. – Ian MacDonald Feb 02 '15 at 18:02
  • @IanMacDonald - I mean the views don't animate on my iPhone6 running iOS8. They do however on my iPhone4 running iOS 7.1.2. – Snowcrash Feb 02 '15 at 18:04
  • I see them animate on my iPhone 6 running iOS 8. Are you implying that they don't move at all, they don't animate their change in position, or Godzilla eats your phone? – Ian MacDonald Feb 02 '15 at 18:05
  • They don't move (and therefore don't animate). Godzilla is, however, eating my brain. – Snowcrash Feb 02 '15 at 18:09
  • Hmm - a slight caveat - I hadn't noticed before but there's a slight flicker. The views do move but it's very odd - they suddenly jump down and then scroll back up to where they started - rather than all scrolling up smoothly to a new location. – Snowcrash Feb 02 '15 at 18:15

1 Answers1

2

If you are using Auto-Layout you should not animate using frames anymore but constraint constants.

Also keep in mind that the "subviews" array property only contains the immediate "children" views. This could affect things, depending on your setup.

Edit I just noticed the problem only occurs on iOS7/iphone 4. There is a known issue with Auto-Layout and iOS7/iOS8 compatibility.

It comes down to how the updates are propagated along the view hierarchy.

There is a number of stackoverflow posts on this subject:

Auto-Layout Issues: iOS 7 vs iOS8

Issue with Auto Layout on iOS 8 (code works perfectly on iOS 7)

Community
  • 1
  • 1
joakim
  • 3,533
  • 2
  • 23
  • 28