0

我有一个 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 并将其设置为自动调整大小的掩码,如下图所示,并且效果很好。

在 Interface Builder 中自动调整大小

我认为当我想要与上图相同的自动调整大小时,我会像下面的代码那样以编程方式进行,但它不会给我相同的结果。相反,它位于距顶部约 2/3 处,这看起来很奇怪,因为我将 y 位置设置self.chatTextView为 327,它位于选项卡栏的正上方。

self.chatTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

当我希望视图以纵向和横向定位在选项卡栏上方时,有谁知道我应该如何设置我的自动调整大小掩码?

编辑:

如果我将自动调整大小掩码更改为 includeUIViewAutoresizingMaskFlexibleBottomMargin而不是UIViewAutoresizingMaskFlexibleTopMargin,则视图在纵向模式下在选项卡栏上方对齐,但在横向模式下消失在屏幕之外。

4

1 回答 1

0

当检测到旋转时,我最终以编程方式定位它。

于 2012-02-20T08:39:37.317 回答