以编程方式将 UIView 定位在任何方向的选项卡栏上方

发布于 2024-12-28 21:54:51 字数 2145 浏览 1 评论 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 并设置它的自动调整大小蒙版,如下图所示,它工作得很好。

Autoresizing in Interface Builder

我认为当我想要与上图相同的自动调整大小时,我会以编程方式执行此操作下面的代码,但它没有给我相同的结果。相反,它位于距顶部约 2/3 的位置,这看起来很奇怪,因为我将 self.chatTextView 的 y 位置设置为 327,即标签栏上方。

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

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

编辑:

如果我更改自动调整大小蒙版以包含UIViewAutoresizingMaskFlexibleBottomMargin而不是UIViewAutoresizingMaskFlexibleTopMargin,则视图在纵向模式下在选项卡栏上方对齐,但消失了在横向模式下超出屏幕。

I have a UIView which I would like to have aligned just above the UITabBar in both portrait and landscape orientation. I would like to do this programmatically.

This is all I have in my UIView subclass, 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;
}

The image view imageViewBackground should fill the entire UIView and therefore I have set it's autoresizing mask to UIViewAutoresizingFlexibleWidth. This works fine.

When initializing the view, I set it's autoresizing mask.

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];

I have tried adding a UIView in Interface Builder and set it's autoresizing mask as in the image below and it works perfectly.

Autoresizing in Interface Builder

I thought that when I want to have the same autoresizing as the image above, I would do it programmatically as the code below but it doesn't give me the same result. Instead it is positioned about 2/3 from the top, which seems odd as I set the y-position of the self.chatTextView to 327 which is just above the tab bar.

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

Does anyone know how I should set my autoresizing mask when I want the view to be positioned above the tab bar in both portrait and landscape orientation?

EDIT:

If I change the autoresizing mask to include UIViewAutoresizingMaskFlexibleBottomMargin instead of UIViewAutoresizingMaskFlexibleTopMargin, the view is aligned above the tab bar in portrait mode but is gone out of the screen in landscape mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把回忆走一遍 2025-01-04 21:54:51

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

I ended up positioning it programmatically when a rotation was detected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文