我想让一个UITextView
填满它的superView
,这个superView
是一个普通的UIView
,位于UIViewController
实例中。
使用API指定的autoresizingMask
和autoresizesSubviews
属性似乎无法使UITextView
做到这一点。即使按照此处所示进行设置,它也无济于事;尽管其superView
填满屏幕,但UITextView
仍然很小。
// 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];
然而,如果我在视图控制器中实例化自己的视图并替换其“.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];
我已经在所有这些初始化器/方法内尝试了这两个代码块,但每种情况都出现了相同的情况:
-(id)init;
-(id)initWithFrame:(CGRect)frame;
-(void)viewDidLoad;
我意识到重新实例化UIViewController
的'.view'很丑陋,有人能解释一下我做错了什么吗?我认为我可以通过在我的UIViewController
中拥有初始框架设置代码来调整UITextView
的大小,然后autoresizing
将按预期操作。
-(void)viewDidLoad {
textView.frame = self.view.frame;
}
...但貌似此时视图的view.frame没有设置,它没有定义'.size'值,因此textView仍然很小。
我想要实现我的目标的正确方法是什么?必须通过UITextView:initWithFrame
明确指定全屏尺寸才能使其填充其父视图吗?
如果您能提供任何建议,我将不胜感激。
init
之前不要使用autorelease
。正确的顺序是...alloc] init] autorelease]
。UIView
应该没问题,但如果你在错误的顺序中使用autorelease
和init
,NSString
和其他类簇将会泄漏。很抱歉无法帮助你的真正问题。 - Yuji