如何使子视图填满其父视图

9
我在这里找不到答案,也没有看到重复的问题。
我的代码很简单。给定3个UIView,父视图、起始视图和目标视图,从父视图中删除起始视图并添加子视图。+ 添加动画,但那只是小玩意。
现在的问题是,当我这样做时,目标视图会被偏移。就好像它被向下推了一样。
我甚至添加了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];*/

}

我已经找到了解决方案。原来 To 的框架是:
To.frames: {{0, 0}, {320, 372}}
但是,如果我去掉 To.frame=ParentView.frame,框架也会变成:
{{0, 20}, {320, 460}}
我想知道为什么?20个点似乎是状态栏的距离。我们在 InterfaceBuilder 中可以在哪里设置该框架?
我在 SimulatedMetric 部分将状态栏设置为无。

5
Objective-C的惯例要求变量以小写字母开头(大写字母仅用于类/协议名称)。请遵循这些惯例。你(未来?)的同事会感激你的。 - Regexident
2个回答

20

2
重点是frame是相对于父视图的父视图的坐标系。bounds是相对于视图本身的坐标系。如果您使用frame,并且父视图在其父级中有偏移,则无法正确工作。 - Michael Mior

0
在Swift中修复子视图和视图底部的“20点”差异:
To.frame = NSRect(origin: CGPoint(x: ParentView.bounds.origin.x, 
                                  y: ParentView.bounds.origin.y-20), 
                    size: ParentView.bounds.size)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接