【发布时间】:2012-02-17 17:05:42
【问题描述】:
我有一个 UIView,我想在 UITabBar 上方以纵向和横向对齐。我想以编程方式执行此操作。
这就是我的 UIView 子类中的全部内容,IPChatTextView:
/* Initialization */
- (id)init
{
if (self = [super init])
{
// Set frame
self.frame = CGRectMake(0, 0, 320, 40);
// Create background
UIEdgeInsets insetsBackground = UIEdgeInsetsMake(20, 50, 19, 82);
UIImage *imageBackground = [[UIImage imageNamed:@"ChatTextView_Background.png"] resizableImageWithCapInsets:insetsBackground];
UIImageView *imageViewBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
imageViewBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth;
imageViewBackground.image = imageBackground;
// Add subviews
[self addSubview:imageViewBackground];
}
return self;
}
图像视图imageViewBackground 应该填满整个 UIView,因此我将它的自动调整大小掩码设置为UIViewAutoresizingFlexibleWidth。这很好用。
初始化视图时,我将其设置为自动调整大小的掩码。
self.chatTextView = [[IPChatTextView alloc] init];
self.chatTextView.frame = CGRectMake(0, 327, 320, 40);
self.chatTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:self.chatTextView];
我尝试在 Interface Builder 中添加一个 UIView 并将其设置为自动调整大小的掩码,如下图所示,并且效果很好。
我想当我想要与上图相同的自动调整大小时,我会像下面的代码那样以编程方式进行,但它不会给我相同的结果。相反,它位于距顶部约 2/3 的位置,这看起来很奇怪,因为我将 self.chatTextView 的 y 位置设置为 327,它位于标签栏的正上方。
self.chatTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
当我希望视图以纵向和横向定位在标签栏上方时,有谁知道我应该如何设置自动调整大小掩码?
编辑:
如果我将自动调整大小的掩码更改为包含 UIViewAutoresizingMaskFlexibleBottomMargin 而不是 UIViewAutoresizingMaskFlexibleTopMargin,则视图在纵向模式下会在标签栏上方对齐,但在横向模式下会消失在屏幕之外。
【问题讨论】:
标签: iphone interface-builder autoresize autoresizingmask