xcode/iOS:自动调整大小以填充视图 - 显式的帧大小是必不可少的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4920798/
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
xcode/iOS: Autoresize to fill a view - explicit frame size is essential?
提问by KomodoDave
I would like a UITextView
to fill its superView
, which is a plain UIView
inside a UIViewController
instance.
我想要一个UITextView
来填充它的superView
,它是UIView
一个UIViewController
实例中的一个普通的。
It seems that I cannot make the UITextView
do this solely by using the API-specified properties of autoresizingMask
and autoresizesSubviews
. Setting these as shown here does nothing; the UITextView
remains small even though its superView
fills the screen.
似乎我无法UITextView
仅通过使用 API 指定的autoresizingMask
和属性来做到这一点autoresizesSubviews
。如下所示设置这些没有任何作用;在UITextView
依然很小,即使它superView
充满屏幕。
// use existing instantiated view inside view controller;
// ensure autosizing enabled
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|
UIViewAutoresizingFlexibleWidth;
// create textview
textView = [[[UITextView alloc] autorelease] initWithFrame:CGRectMake(0, 0, 1, 1)];
// enable textview autoresizing
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleHeight];
// add textview to view
[self.view addSubview:textView];
However, if I instantiate my own view inside the view controller, replacing its '.view' property, then everything works as expected, and the textView fills its superview:
但是,如果我在视图控制器中实例化我自己的视图,替换它的 '.view' 属性,那么一切都按预期工作,并且 textView 填充了它的超级视图:
// reinstantiate view inside view controller
self.view = [[UIView alloc]init];
// create textview
textView = [[[UITextView alloc] autorelease] initWithFrame:CGRectMake(0, 0, 1, 1)];
// enable textview autoresizing
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleHeight];
// add textview to view
[self.view addSubview:textView];
I've tried both code chunks inside all of these initialisers/methods, and the same situation arises in every case:
我已经在所有这些初始化程序/方法中尝试了两个代码块,并且在每种情况下都会出现相同的情况:
-(id)init;
-(id)initWithFrame:(CGRect)frame;
-(void)viewDidLoad;
I realise that reinstantiating the '.view' of the UIViewController
is fugly, can anyone explain what I'm doing wrong? I presumed I could overcome the issue by having initial frame-setting code in my UIViewController
to resize the UITextView
once, and thereafter autoresizing
would operate as desired.
我意识到重新实例化 '.view'UIViewController
很糟糕,谁能解释我做错了什么?我认为我可以通过在我的初始框架设置代码中UIViewController
调整UITextView
一次大小来克服这个问题,然后autoresizing
可以根据需要进行操作。
-(void)viewDidLoad {
textView.frame = self.view.frame;
}
... but seemingly the view.frame is not set up at this stage, it does not have its '.size' values defined, so once again textView remains small.
...但看起来 view.frame 在这个阶段没有设置,它没有定义它的 '.size' 值,所以 textView 再次保持小。
What's the proper way of achieving what I want? Must I explicitly specify the fullscreen dimensions via UITextView:initWithFrame
to get it to fill its superview?
实现我想要的正确方法是什么?我必须通过明确指定全屏尺寸UITextView:initWithFrame
才能让它填充其超级视图吗?
I'd be grateful for any advice you can offer.
如果您能提供任何建议,我将不胜感激。
回答by Ole Begemann
Autoresizing does not mean that the subview will take up the size of its superview. It just means that it will resize relative to the size change of its superview whenever the superview's bounds change. So initially, you have to set the size of the subview to the correct value. The autoresizing mask will then deal with size changes in the future.
自动调整大小并不意味着子视图将占据其父视图的大小。这只是意味着只要父视图的边界发生变化,它就会相对于其父视图的大小变化调整大小。所以最初,您必须将子视图的大小设置为正确的值。自动调整大小掩码将在将来处理大小更改。
This is all you need:
这就是你所需要的:
textView = [[[UITextView alloc] autorelease] initWithFrame:self.view.bounds];
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleHeight];
回答by Philippe H. Regenass
And here Swift 3solution:
这里是Swift 3解决方案:
myView.autoresizingMask = [.flexibleWidth, .flexibleHeight]