ios iPhone:如何设置 UIViewController 框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4929578/
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
iPhone: How to set UIViewController frame?
提问by Nic Hubbard
I have a root UIViewController that I am adding other UIViewController's as subviews. Currently each of the subviews are too low down (covering up my custom build tabbar). When I try to so something like the following, it does not work:
我有一个根的UIViewController,我加入其他UIViewController的子视图作为。目前每个子视图都太低了(覆盖了我的自定义构建标签栏)。当我尝试执行以下操作时,它不起作用:
// Test setting frame size to see if it works
self.view.frame = CGRectMake(0, 0, 200, 200);
That does nothing to change the frame size.
这不会改变帧大小。
So, my question is, how can I set my frame when the UIViewController is initialized after it is added as the subview?
所以,我的问题是,当 UIViewController 添加为子视图后初始化时,如何设置我的框架?
回答by Robin
@Nic i think when you are adding that other view, at that time you should define the other views frame size like this:
@Nic 我认为当你添加其他视图时,你应该像这样定义其他视图的框架大小:
Someviewcontroller *c = initWithNibName
c.view.frame = CGRectMake(0, 0, 200, 200);
[self addSubView:c];
i dont know if this will work but it is something like this.
我不知道这是否可行,但它是这样的。
回答by Chris Zelenak
First, ensure that your frame is actually not changing size. Likely it /is/ changing size, but you are expecting it to clip its contents; this behavior is not enabled by default on a UIView, and would need to be set via:
首先,确保您的框架实际上没有改变大小。可能它 /is/ 改变大小,但你期望它剪裁它的内容;UIView 默认情况下未启用此行为,需要通过以下方式设置:
[[self view] setClipsToBounds:YES];
To double check and ensure that your frame is / is not changing size after setting the new frame, try logging this:
仔细检查并确保你的框架是设置新的帧后/不改变大小,尝试登录此:
NSLog(@"New frame is: %@", NSStringFromCGRect([[self view] frame]));
or simply setting a breakpoint after the change and inspecting the value in your debugger.
或者只是在更改后设置断点并检查调试器中的值。
回答by Paul Brady
Then there's always...
然后总是...
// Screen, less StatusBar
CGRect l_RectFrame = [UIScreen mainScreen].applicationFrame;
OR...
或者...
// Entire Screen
CGRect l_RectFrame = [UIScreen mainScreen].bounds;
MyView *l_aView = [[MyView alloc] initWithFrame:l_RectFrame];
...