ios 设置 UIView 的框架不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18263359/
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
Setting the frame of an UIView does not work
提问by MyJBMe
I have a problem with the frame-property in iOS 7.
I wanna resize some UIViews in the viewDidLoad
-method of my UIViewController
, but if I do it like int screenHeight = [[UIScreen mainScreen] bounds].size.height;
[self.leftSideTableView setFrame: CGRectMake(0, 0, 320, screenHeight)];
the height is set as I want it till the end of the method, but in every other method it is as is has been before!
我在 iOS 7 中的 frame-property 有问题。我想在viewDidLoad
我的-method 中调整一些 UIViews 的大小UIViewController
,但如果我这样做,int screenHeight = [[UIScreen mainScreen] bounds].size.height;
[self.leftSideTableView setFrame: CGRectMake(0, 0, 320, screenHeight)];
高度设置为我想要的直到方法结束,但在其他所有方法和以前一样!
What's wrong with it or is it just a bug of the compiler or anything else?
它有什么问题还是只是编译器或其他任何东西的错误?
回答by MyJBMe
One has to put view resizing into -viewDidLayoutSubviews:
! (documentation)
必须将视图调整大小放入-viewDidLayoutSubviews:
! (文档)
Placing view frame changes into -viewWillAppear:
or -viewDidLoad:
will not work, because the views are not laying out yet!
将视图框架更改为-viewWillAppear:
或-viewDidLoad:
将不起作用,因为视图尚未布局!
回答by liuyaodong
Check if you are using
autolayout
in your xib file. If you don't want to useautolayout
, uncheck it in your xib file.Change your
self.leftSideTableView
frame in-viewWillAppear:
.
检查您是否
autolayout
在 xib 文件中使用。如果您不想使用autolayout
,请在您的 xib 文件中取消选中它。改变你
self.leftSideTableView
的框架-viewWillAppear:
。
回答by lancejabr
Check to make sure that auto layout isn't activated in your storyboard file.
检查以确保您的故事板文件中未激活自动布局。
To turn it off, look at the inspector in interface builder. Click the icon that looks like a page all the way on the left. In the section "Interface Builder Document" uncheck "Use Auto Layout."
要关闭它,请查看界面构建器中的检查器。单击左侧看起来像一个页面的图标。在“Interface Builder Document”部分,取消选中“Use Auto Layout”。
I find it's best to an entire view controller in IB with auto layout, or completely in code. Mixing the two can lead to weird behavior that is hard to debug.
我发现最好在 IB 中使用自动布局或完全在代码中的整个视图控制器。将两者混合会导致难以调试的奇怪行为。
回答by Mick MacCallum
There are several reasons why this might be happening. First of all, you need to make sure that your tableview isn't nil. If you're creating it programmatically, you need to be sure that you're calling alloc/init somewhere before you attempt to set the frame. If self.leftSideTableView
is an IBOutlet, this can be caused by forgetting to actually link the outlet to the interface object.
发生这种情况的原因有多种。首先,您需要确保您的 tableview 不为零。如果您以编程方式创建它,则需要确保在尝试设置框架之前在某处调用 alloc/init。如果self.leftSideTableView
是 IBOutlet,这可能是由于忘记将插座实际链接到接口对象造成的。
Then, second and less likely, you are creating the table view programmatically and initializing it properly, but you forgot to add it as a subview of one of your on screen views.
然后,第二个不太可能的是,您正在以编程方式创建表视图并正确初始化它,但是您忘记将其添加为屏幕视图之一的子视图。