0

Facing a really strange issue trying to dynamically add a left navigation panel to a View Controller, (should be able to support any view controller in the app, vaguely similar to the Facebook navigation) My idea seemed fairly simple, but I'm really not seeing where it's breaking down. What I've done is created a Category on UIViewController with the following method which I would think would move all the subviews to the right, and then add the new view.

-(void)addLeftView:(UIView *)newView
{
   newView.frame=CGRectMake(0, 0, newView.frame.size.width, self.view.frame.size.height);

   for(UIView *view in [self.view subviews])
   {
     view.frame=CGRectMake(view.frame.origin.x + newView.frame.size.width, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
   }

   [self.view addSubview:newView];
}

What actually happens, though, is that the view is added, but the subviews do not move to the right. However, if you comment the addSubview out, everything actually does move to the right exactly as expected.

To make matters even weirder, if you wrap the view movement in a [UIView animateWithDuration:completionHandler:], where the completion handler adds the subview, the animation actually happens - all the views shift to the right, but when the subview gets added, they jump back to their starting position.

I assumed this was some sort of wacky auto-layout issue, so just to see what happened, I cleared all the constraints out of that view controller, and get the same result.

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
Josh Edson
  • 145
  • 5
  • Did you try turning off auto layout? Cleaning them wouldn't work, because the system automatically adds them if you don't. This is exactly the kind of effect that you get if you set frames directly with auto layout turned on -- when something causes the view to be redrawn, the views move back to the positions determined by their constraints. – rdelmar Dec 19 '13 at 01:10
  • Unfortunately, in their infinite wisdom, Apple decided not to let you turn off auto-layout selectively. Everything seems to be fine in the other dozen view controllers in the app, so turning it all the way off seems like overkill. I guess I'll just have to move that one VC to its own Storyboard and go that way. Thanks for the help! – Josh Edson Dec 19 '13 at 17:00
  • I didn't mean to turn it off permanently, just to test whether auto layout was causing the problem. If it is, you should fix the problem by adjusting constraints rather than setting frames in the addLeftView method. – rdelmar Dec 19 '13 at 17:05
  • Indeed, turning off auto-layout seems to make the issue go away. Guess it's just a matter of getting used to playing with the constraints programmatically instead of explicitly setting frames. Thanks again! – Josh Edson Dec 19 '13 at 17:41

1 Answers1

0

Found the answer in a similar but un-related thread. Can I disable autolayout for a specific subview at runtime?

Basically it was auto-layout reverting my positioning, so disabling it by setting this user variable on just the UIViews of problem fixed my issues.

You can set the translatesAutoresizingMaskIntoConstraints type Boolean, Value to Yes in the User Defined Runtime Attributes of the UIView you want in the xib/storyboard.

Community
  • 1
  • 1
TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30