xcode 如何让子视图填充其父视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6402271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a subview fill its superview
提问by user4951
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.
我的代码很简单。给定 3 个 UIView,父视图,从和到,从父视图中删除并添加子视图。+ 添加动画,但这只是小玩意。
Now, the problem is when I do that, the to then get offsetted. It's as if it's pushed down.
现在,问题是当我这样做时,to 就会被抵消。就好像被压下去了一样。
I even add To.frame=ParentView.frame; to make sure it works. It doesn't.
我什至添加 To.frame=ParentView.frame; 以确保它有效。它没有。
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 的框架是
To.frames: {{0, 0}, {320, 372}}
To.frames: {{0, 0}, {320, 372}}
However, if I remove To.frame=ParentView.frame the frame is also
但是,如果我删除 To.frame=ParentView.frame 框架也是
{{0, 20}, {320, 460}}
{{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?
我想知道为什么?20点好像是状态栏的距离。我们在哪里可以在 InterfaceBuilder 中设置该框架?
I set statusbar to none in SimulatedMetric section.
我在 SimulatedMetric 部分将 statusbar 设置为 none。
回答by cxa
Seems you misunderstand the frame and bounds, revise your code to To.frame=ParentView.bounds;
should be OK.
看来你对frame和bounds有误解,修改你的代码To.frame=ParentView.bounds;
应该就可以了。
Related: Why is there an frame rectangle and an bounds rectangle in an UIView?
回答by Daniel Valeriev
Fix "20 points
" difference from bottom in subView and View in Swift:
修复 " 20 points
" 与 subView 中的底部和 Swift 中的 View 的差异:
To.frame = NSRect(origin: CGPoint(x: ParentView.bounds.origin.x,
y: ParentView.bounds.origin.y-20),
size: ParentView.bounds.size)