如何让子视图填充其父视图

发布于 2024-11-16 09:41:19 字数 1340 浏览 3 评论 0原文

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

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

现在的问题是,当我这样做时, to 就会被抵消。就好像被压下去了一样。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久而酒知 2024-11-23 09:41:19

看来您误解了框架和边界,将代码修改为 To.frame=ParentView.bounds; 应该没问题。

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

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?

红尘作伴 2024-11-23 09:41:19

修复 Swift 中子视图和视图底部的“20 点”差异:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文