我有10个UIButtons彼此相同。我想根据iPhone的屏幕尺寸改变它的高度。它应该在iPhone 6 plus屏幕中看起来更大,在iPhone 5s屏幕中看起来更小。如何使用autolayout进行。
答案 0 :(得分:1)
首先选择一个UIView并设置其约束,如顶部,底部,前导和尾随,然后拖动视图上的所有UIButton并设置所有按钮约束,如顶部,底部,前导,尾随和相等的宽度和相等的高度限制你可以检查这些图像 iPhone 7 Plus屏幕: -
答案 1 :(得分:1)
为此,您必须根据设备屏幕大小的百分比(%)添加每个按钮的高度。因此按钮大小可能因设备(iPhone4s,5s,6 plus)屏幕尺寸而异。
现在我将使用KVConstraintExtensionsMaster
库以编程方式添加约束。通过从ViewController的viewDidLoad
调用以下方法来尝试以下代码。
- (void)configureScrollViewHierarchyAndApplyConstraint
{
CGFloat mainScreenHeight = [[UIScreen mainScreen] bounds].size.height;
// here baseScreenHeight is default screen height size for which we are implementing.
CGFloat baseScreenHeight = 667; // here default iPhone 6 height
// Note: try by changing baseScreenHeight via any iPhone screen height(480, 568, 667, 736) and see the changes in button height & space
// here fixed space and height are fixed size with respect to iPhone 6 height.
CGFloat fixedSpace = 28;
CGFloat fixedHeight = 150;
// ratio is responsible to increase or decrease button height depends on iPhone device size.
CGFloat ratio = mainScreenHeight/baseScreenHeight;
CGFloat baseSpace = fixedSpace * ratio;
CGFloat baseHeight = fixedHeight * ratio;
// prepare scrollView for autolayout
UIScrollView *scrollView = [UIScrollView prepareNewViewForAutoLayout];
scrollView.backgroundColor = [UIColor brownColor];
[self.view addSubview:scrollView];
// prepare containerView for autolayout
UIView *containerView = [UIView prepareNewViewForAutoLayout];
containerView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.95];
[scrollView addSubview:containerView];
// To add Leading and Trailing constraint
[scrollView applyLeadingAndTrailingPinConstraintToSuperviewWithPadding:baseSpace];
// To add Top and Bottom constraint
[scrollView applyTopAndBottomPinConstraintToSuperviewWithPadding:baseSpace];
// To add Top and Bottom constraint of containerView
[containerView applyTopAndBottomPinConstraintToSuperviewWithPadding:0];
// To Define the containerView X Position by adding HorizontalCenter constraint
[containerView applyConstraintForHorizontallyCenterInSuperview];
// Here To Define the width
[containerView applyEqualWidthPinConstrainToSuperview]; // Or
//[containerView applyLeadingPinConstraintToSuperviewWithPadding:10];
NSInteger count = 20;
UIButton *previousContentButton = nil;
for (NSInteger i = 0; i < count; i++)
{
UIButton *contentButton = [UIButton prepareNewViewForAutoLayout];
if (i&1) {
[contentButton setBackgroundColor:[UIColor greenColor]];
}else{
[contentButton setBackgroundColor:[UIColor redColor]];
}
[contentButton setTag:i];
[containerView addSubview:contentButton];
// Define the contentButton Size
[contentButton applyLeadingAndTrailingPinConstraintToSuperviewWithPadding:baseSpace];
[contentButton applyHeightConstraint:baseHeight];
if (i == 0) // for first
{
// To add top constraint
[contentButton applyTopPinConstraintToSuperviewWithPadding:baseSpace];
}
else if (i == count-1) // for last
{
// To add vertical constraint between two buttons
[previousContentButton applyConstraintFromSiblingViewAttribute:NSLayoutAttributeBottom toAttribute:NSLayoutAttributeTop ofView:contentButton spacing:baseSpace];
// To add bottom constraint
[contentButton applyBottomPinConstraintToSuperviewWithPadding:baseSpace];
}
else
{
// To add vertical constraint between two buttons
[previousContentButton applyConstraintFromSiblingViewAttribute:NSLayoutAttributeBottom toAttribute:NSLayoutAttributeTop ofView:contentButton spacing:baseSpace];
}
previousContentButton = contentButton;
}
[containerView updateModifyConstraints];
}
答案 2 :(得分:0)
一个简单的例子:
您可以查看answer。
答案 3 :(得分:0)
只需使用Verticle UIStackView。