如何使子视图填充其超级视图

use*_*951 9 xcode objective-c subview superview addsubview

无论如何我在这里找不到答案,也没有看到重复的问题.

我的代码很简单.给定3 UIView,parent,from和to,从parent中删除并添加子视图.+添加动画,但这只是doodads.

现在,问题是当我这样做时,然后得到抵消.好像它被推倒了.

我甚至添加了To.frame = ParentView.frame; 确保它有效.它没有.

我该怎么办?

+ (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];*/

}
Run Code Online (Sandbox Code Playgroud)

我已经找到了解决方案.结果是To的框架

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

但是,如果我删除To.frame = ParentView.frame,那么框架也是如此

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

我想知道为什么?20点似乎是状态栏的距离.我们在哪里可以在InterfaceBuilder中设置该框架?

我在SimulatedMetric部分中将statusbar设置为none.

cxa*_*cxa 20

好像你误解了框架和边界,修改你的代码To.frame=ParentView.bounds;应该没问题.

相关:为什么UIView中有框架矩形和边界矩形?

  • 关键是`frame`位于superview的superview的坐标中.`bounds`位于视图的坐标中.如果使用`frame`并且superview在其父级内偏移,则无法正常工作. (2认同)