我有一个奇怪的问题,可能有一个非常简单的解决方案,我只是看不到它......
我有一个 UIView 类,其中包含一个按钮作为 subview ,跨越 View 宽度的 4/5 和高度的三分之一。当父 View 调整大小(例如,具有高度和一半宽度)时,按钮应保持在原来的位置(父 View 的顶部三分之一),并且现在也具有一半宽度和新高度的三分之一。
这意味着:
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)
问题是,按钮后面总是有框架(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 标志的一些变体,但没有效果 - 并且上面的标志也应该是正确的......
最佳答案
这是您需要做的:
button.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin;
如果您设置灵活的高度和宽度而不设置灵活的边距,则调整大小时会发生的情况是高度和宽度发生变化,以便边距(即到 super View 边界的距离)保持不变。
将高度和底部边距设置为灵活,可以使顶部边距保持不变,并且 View 将按照父 View 的比例调整大小。宽度和右边距也是如此。
关于iphone - 自动调整 subview 大小 : resize seems to happen absolute instead of proportional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329345/