9

I can't find the answer here anyway, nor do I see a duplicate question.

My code is simple. Given 3 UIView, parent, from and to, remove from from parent and add subview. + add animation but that's just doodads.

Now, the problem is when I do that, the to then get offsetted. It's as if it's pushed down.

I even add To.frame=ParentView.frame; to make sure it works. It doesn't.

What should I have done?

+ (void) AnimateSwitchingWithParent: (UIView *) ParentView From: (UIView *) From To: (UIView* ) To
{
    /*[UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];


    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:ParentView cache:YES];*/
    [From removeFromSuperview];
    [ParentView addSubview:To];

    To.frame=ParentView.frame; //If I don't do this, the subview is still off by around 20 points
    NSLog(@"To.bounds: %@", NSStringFromCGRect(To.bounds));
    NSLog(@"From.bounds.: %@", NSStringFromCGRect(From.bounds));
    NSLog(@"ParentView.bounds: %@", NSStringFromCGRect(ParentView.bounds));


    //JA_DUMP (To.bounds);
    //JA_DUMP (To.bounds);



    /*[UIView commitAnimations];*/

}

I have figured out the solution. Turns out the frame of To is

To.frames: {{0, 0}, {320, 372}}

However, if I remove To.frame=ParentView.frame the frame is also

{{0, 20}, {320, 460}}

I wonder why? 20 points seem to be the distance of the status bar. Where can we set that frame in InterfaceBuilder?

I set statusbar to none in SimulatedMetric section.

0x6A75616E
  • 4,696
  • 2
  • 33
  • 57
user4951
  • 32,206
  • 53
  • 172
  • 282
  • 5
    Objective-C conventions expect variables to start with a lowercase letter (uppercase is for class/protocol names only). Please stick to them. Your (future?) co-workers will thank you for it. – Regexident Jun 19 '11 at 12:17

2 Answers2

20

Seems you misunderstand the frame and bounds, revise your code to To.frame=ParentView.bounds; should be OK.

Related: Why is there an frame rectangle and an bounds rectangle in an UIView?

Community
  • 1
  • 1
cxa
  • 4,238
  • 26
  • 40
  • 2
    The point is that the `frame` is in the coordinates of the superview's superview. `bounds` is in the coordinates of the view. If you use `frame` and the superview is offset within it's parent, this won't work correctly. – Michael Mior Sep 18 '12 at 15:19
0

Fix "20 points" difference from bottom in subView and View in Swift:

To.frame = NSRect(origin: CGPoint(x: ParentView.bounds.origin.x, 
                                  y: ParentView.bounds.origin.y-20), 
                    size: ParentView.bounds.size)
Daniel Valeriev
  • 358
  • 2
  • 8