【问题标题】:How to make a subview fill its superview如何使子视图填充其父视图
【发布时间】:2011-06-19 12:10:53
【问题描述】:

反正我在这里找不到答案,也没有看到重复的问题。

我的代码很简单。给定 3 个 UIView,parent,from 和 to,从 parent 中删除并添加 subview。 + 添加动画,但这只是小玩意儿。

现在,问题是当我这样做时,然后会被抵消。好像被推下去了一样。

我什至添加了 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 部分将状态栏设置为无。

【问题讨论】:

  • Objective-C 约定要求变量以小写字母开头(大写仅用于类/协议名称)。请坚持他们。您的(未来?)同事会为此感谢您。

标签: objective-c xcode subview addsubview superview


【解决方案1】:

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

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

【讨论】:

  • 重点是frame在superview的superview坐标中。 bounds 在视图的坐标中。如果你使用 frame 并且父视图在它的父视图中偏移,这将无法正常工作。
【解决方案2】:

修复“20 points”在 subView 和 Swift 中与底部的差异:

To.frame = NSRect(origin: CGPoint(x: ParentView.bounds.origin.x, 
                                  y: ParentView.bounds.origin.y-20), 
                    size: ParentView.bounds.size)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2013-10-27
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多