加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xcode / iOS:自动调整以填充视图 – 显式帧大小是必不可少的?

发布时间:2020-12-15 01:46:08 所属栏目:百科 来源:网络整理
导读:我想要一个UITextView来填充它的superView,它是一个UIViewController实例中的普通UIView. 似乎我无法通过使用autoresizingMask和autoresizesSubviews的API指定属性来使UITextView完成此操作.如此处所示设置这些没有任何作用;即使superView填充屏幕,UITextVie
我想要一个UITextView来填充它的superView,它是一个UIViewController实例中的普通UIView.

似乎我无法通过使用autoresizingMask和autoresizesSubviews的API指定属性来使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,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,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的大小来解决这个问题,然后自动调整将按需运行.

-(void)viewDidLoad {
    textView.frame = self.view.frame;
}

…但是貌似view.frame没有在这个阶段设置,它没有定义’.size’值,所以textView仍然很小.

实现我想要的正确方法是什么?我必须通过UITextView:initWithFrame明确指定全屏尺寸,以使其填充其超视图吗?

对于您提供的任何建议,我将不胜感激.

解决方法

自动调整并不意味着子视图将占用其超级视图的大小.它只是意味着只要superview的边界发生变化,它就会相对于superview的大小变化进行调整.因此,首先,您必须将子视图的大小设置为正确的值.然后,自动调整掩码将处理将来的大小更改.

这就是你所需要的:

textView = [[[UITextView alloc] autorelease] initWithFrame:self.view.bounds];
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
                              UIViewAutoresizingFlexibleHeight];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读