自动调整子视图大小:调整大小似乎是绝对的而不是按比例的

发布于 2025-01-06 18:54:39 字数 1067 浏览 0 评论 0原文

我有一个奇怪的问题,可能有一个非常简单的解决方案,我只是看不到它...

我有一个 UIView 类,其中包含一个按钮作为子视图,跨越宽度的 4/5 和视图的三分之一高度。当父视图调整大小时(例如,具有高度和宽度的一半),按钮应保持在原来的位置(父视图的顶部三分之一),现在也具有一半的宽度和新高度的三分之一。

这意味着:

Parent before: Frame (0,0,100,100)
Button before: Frame (0,0,80,33.3)

Parent after: Frame (0,0,50,50)
Button after: Frame (0,0,40,16.7)

问题是,按钮之后总是有 Frame (0,0,30,0)。看起来好像按钮的新宽度和高度是通过减去 50 像素的变化来实现的,而不是按比例调整大小。一定是一些非常愚蠢的东西,但我就是找不到它。

这是我的 UIView 类的代码:

-(id)init
{
    self = [super init];
    if(self)
    {
        self.autoresizesSubviews = true;

        self.frame = CGRectMake(0,0,100,100);
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0,0,80, self.frame.size.height / 3);
        button.autoresizingMask = UIViewAutoresizingFlexibleHeight
                                  |UIViewAutoresizingFlexibleWidth;

        [self addSubview:button];
    }

    return self;
}

有人有想法吗?我已经尝试了 autoresizeMask 标志的一些变体,但没有效果 - 并且上面的标志也应该是正确的......

I have a strange problem which probably has a very simple solution, I just can't see it ...

I have an UIView class which contains a button as a subview, spanning the 4/5th of the width and one third of the view's height. When the parent view is resized (eg have the height and half the width), the button should stay where it is (top third of the parent view), and now also have half the width and still one third of the new height.

That means:

Parent before: Frame (0,0,100,100)
Button before: Frame (0,0,80,33.3)

Parent after: Frame (0,0,50,50)
Button after: Frame (0,0,40,16.7)

Problem is, button after always has the Frame (0,0,30,0). It seems as if the new width and height of the button i just achieved by subtracting the 50 pixel change, instead of doing a proportional resize. Must be something really stupid, but I just can't find it.

Here is the code of my UIView class:

-(id)init
{
    self = [super init];
    if(self)
    {
        self.autoresizesSubviews = true;

        self.frame = CGRectMake(0,0,100,100);
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0,0,80, self.frame.size.height / 3);
        button.autoresizingMask = UIViewAutoresizingFlexibleHeight
                                  |UIViewAutoresizingFlexibleWidth;

        [self addSubview:button];
    }

    return self;
}

Anyone an idea? I already tried quite some variations on the autoresizeMask flags, but no avail - and the above flags should also be the right ones ...

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

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

发布评论

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

评论(1

笔落惊风雨 2025-01-13 18:54:39

这就是您需要做的:

button.autoresizingMask = UIViewAutoresizingFlexibleHeight |
                          UIViewAutoresizingFlexibleWidth |
                          UIViewAutoresizingFlexibleRightMargin |
                          UIViewAutoresizingFlexibleBottomMargin;

如果您设置灵活的高度和宽度而不设置灵活的边距,则调整大小时会发生的情况是高度和宽度发生变化,以便边距(即到超级视图边界的距离)保持不变。

将高度和底部边距设置为灵活可以使顶部边距保持不变,并且视图将按照父视图的比例调整大小。宽度和右边距也是如此。

This is what you need to do:

button.autoresizingMask = UIViewAutoresizingFlexibleHeight |
                          UIViewAutoresizingFlexibleWidth |
                          UIViewAutoresizingFlexibleRightMargin |
                          UIViewAutoresizingFlexibleBottomMargin;

If you set flexible height and width without setting flexible margins, what happens when you resize is that the height and width change so that the margins (i.e., the distance to the superview's bounds) stays constant.

Setting the height and bottom margin to be flexible makes the top margin stay the same, and the view will resize in proportion to the superview. Same goes for width and right margin.

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