UIView 子类不会自动调整大小 [英] UIView subclass won't autoresize
问题描述
我一直在寻找有关调整大小的背景信息,但找不到太多.我知道我需要在超级视图上设置 autoresizesSubviews
并在子视图上设置 autoresizingMask
.
I've been looking for background information on resizing, but I haven't been able to find much. I know I need to set autoresizesSubviews
on the superview and autoresizingMask
on the subview.
我已经这样做了,我的 UIImageViews 正确调整了大小,但我的自定义 UIView 子类没有.
I have done this, and my UIImageViews properly resize, but my custom UIView subclass does not.
UIImageView 的代码:
The code for the UIImageView:
UIImageView *newX = [[[UIImageView alloc] initWithImage:dot] autorelease];
[newX setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[newX setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 1)];
[self.view addSubview:newX];
自定义 UIView 子类的代码:
The code for the custom UIView subclass:
trace = [[TraceView alloc] initWithFrame:self.view.bounds];
[trace setAutoresizingMask:(UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin)];
[self.view addSubview:trace];
据我所知,视图按预期旋转,但不会调整大小.我再也看不到它的底部,它也没有填满右边的屏幕.我是否必须向 UIView 子类添加任何内容才能正确调整大小?
From what I can tell, the view rotates as expected, but does not resize. I can no longer see the bottom of it, and it doesn't fill the screen to the right. Do I have to add anything to the UIView subclass to make resizing work properly?
我已将 autoresizingMake 更改为 (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)
并调用 [subview setNeedsDisplay];
我不知道为什么这是必要的,因为 UIImageViews 没有它就可以正常工作,但现在它的行为符合预期.
I've changed the autoresizingMake to (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)
and called [subview setNeedsDisplay];
I'm not sure why this is necessary as the UIImageViews work fine without it, but it now behaves as expected.
推荐答案
我认为您缺少子视图上的 UIViewAutoresizingFlexibleHeight
或宽度.否则我认为视图只是根据边距移动但不会调整大小.
I think you're missing UIViewAutoresizingFlexibleHeight
or width on the subview. Otherwise I think the view just moves around according to the margins but doesn't resize.
这篇关于UIView 子类不会自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!