UIButton setAutoresizingMask 无法以编程方式工作
UIButton setAutoresizingMask not working programmatically
我在代码中添加了一个 UIButton,但是当我在不同的设备上测试它时,该按钮没有正确地调整大小和重新定位,但是当我在 Storyboard 上添加相同的按钮时,它的行为正确...故事板没有启用自动布局...
EDIT3:在下面添加了 UI 的照片,较大的 "Basics 2" 按钮是故事板定义的一个....其余的在代码中定义
EDIT2:当我查看故事板 XML 时,按钮的行为正确,它的定义如下,完整的故事板 XML 现在在下面
<button hidden="YES" autoresizesSubviews="NO" opaque="NO" contentMode="center" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" id="ujO-uz-KyO">
<rect key="frame" x="30" y="100" width="116" height="70"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
<inset key="imageEdgeInsets" minX="0.0" minY="27" maxX="72" maxY="0.0"/>
<state key="normal" image="greenp.png" backgroundImage="basics2.png">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
</button>
我这样添加按钮(编辑为使用下面建议的代码,仍然不起作用)
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = [[butLocArr objectAtIndex:x] CGRectValue];
button1.autoresizesSubviews = YES;
[button1 setAutoresizingMask: UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight];
//The view is a UiScrollView
[scrollChallengeView addSubview:button1];
情节提要添加按钮,行为正确,是这样设置的,我哪里错了?
你需要使用UIViewAutoresizingFlexibleMargins
即
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleMargins];
或
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin];
改为
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
在 IB 设置中,您有:
- 灵活的边距(顶部、左侧、底部和右侧);
- 灵活的尺寸(宽度和高度);
所以自动调整掩码应该是
button1.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
设置UIViewAutoresizingFlexibleWidth
和UIViewAutoresizingFlexibleHeight
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
当您使用 AutoresizingMask 在 IB 中添加按钮时,它会在午餐后自动调整大小。例如,屏幕宽度从 320(在故事板中)增加到 540px(在设备上)并且按钮大小按比例增加。当您以编程方式添加按钮时,它只是添加,您应该根据屏幕大小手动计算按钮大小。自动调整按钮将在旋转时调用。所以你的宽度和高度应该这样计算:
CGFloat width = k1 * [UIScreen mainScreen].bounds.size.width;
CGFloat height = k2 * width;
我在代码中添加了一个 UIButton,但是当我在不同的设备上测试它时,该按钮没有正确地调整大小和重新定位,但是当我在 Storyboard 上添加相同的按钮时,它的行为正确...故事板没有启用自动布局...
EDIT3:在下面添加了 UI 的照片,较大的 "Basics 2" 按钮是故事板定义的一个....其余的在代码中定义
EDIT2:当我查看故事板 XML 时,按钮的行为正确,它的定义如下,完整的故事板 XML 现在在下面
<button hidden="YES" autoresizesSubviews="NO" opaque="NO" contentMode="center" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" id="ujO-uz-KyO">
<rect key="frame" x="30" y="100" width="116" height="70"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
<inset key="imageEdgeInsets" minX="0.0" minY="27" maxX="72" maxY="0.0"/>
<state key="normal" image="greenp.png" backgroundImage="basics2.png">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
</button>
我这样添加按钮(编辑为使用下面建议的代码,仍然不起作用)
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = [[butLocArr objectAtIndex:x] CGRectValue];
button1.autoresizesSubviews = YES;
[button1 setAutoresizingMask: UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight];
//The view is a UiScrollView
[scrollChallengeView addSubview:button1];
情节提要添加按钮,行为正确,是这样设置的,我哪里错了?
你需要使用UIViewAutoresizingFlexibleMargins
即
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleMargins];
或
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin];
改为
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
在 IB 设置中,您有:
- 灵活的边距(顶部、左侧、底部和右侧);
- 灵活的尺寸(宽度和高度);
所以自动调整掩码应该是
button1.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
设置UIViewAutoresizingFlexibleWidth
和UIViewAutoresizingFlexibleHeight
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
当您使用 AutoresizingMask 在 IB 中添加按钮时,它会在午餐后自动调整大小。例如,屏幕宽度从 320(在故事板中)增加到 540px(在设备上)并且按钮大小按比例增加。当您以编程方式添加按钮时,它只是添加,您应该根据屏幕大小手动计算按钮大小。自动调整按钮将在旋转时调用。所以你的宽度和高度应该这样计算:
CGFloat width = k1 * [UIScreen mainScreen].bounds.size.width;
CGFloat height = k2 * width;