首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIButton高度随屏幕大小变化

UIButton高度随屏幕大小变化
EN

Stack Overflow用户
提问于 2016-11-22 13:04:00
回答 4查看 2K关注 0票数 0

我有10个UIButtons,一个比另一个低。我想根据iPhone的屏幕尺寸改变它的高度。它在iPhone 6 plus屏幕上应该看起来更大,在iPhone 5s屏幕上看起来更小。如何使用autolayout来实现。

EN

回答 4

Stack Overflow用户

发布于 2016-11-22 13:31:09

你首先选择一个UIView并设置它的顶部,底部,前导和拖尾等约束,然后拖动视图上的所有UIButtons并设置所有按钮约束,如顶部,底部,前导,拖尾和等宽等高约束你可以查看这些图像iPhone 7Plus屏幕:

和iPhone 5s屏幕

Xcode视图

票数 1
EN

Stack Overflow用户

发布于 2016-12-01 04:10:57

为此,您必须根据设备屏幕大小的百分比(%)添加每个按钮的高度。因此按钮大小可以根据设备(iPhone4s、5s、6 plus)屏幕大小而变化。

现在,我将使用KVConstraintExtensionsMaster库以编程方式添加约束。通过从ViewController的viewDidLoad调用下面的方法来尝试下面的代码。

代码语言:javascript
复制
- (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];

}
票数 1
EN

Stack Overflow用户

发布于 2016-11-22 13:12:34

一个简单的例子:

在storyboard

  • ctrl中将一个按钮从对象库拖动到您的视图控制器视图中,从您的按钮拖动到您的视图中(向左或向右拖动),并选择在
  1. 中垂直居中
  2. ctrl从您的按钮拖动到您的视图中(拖动到顶部或底部),并在容器中选择水平居中
  3. ctrl从按钮拖动到按钮(是的,相同的按钮),然后在大小检查器中选择aspect ratio
  4. 选中按钮的宽高比约束,使其具有1:1的乘数
  5. ctrl从按钮拖动到视图(向左或向右拖动),然后选择相等宽度7 .in大小检查器选中按钮的等宽约束,使其具有1:3的乘数(或您喜欢的任何值- 1:3表示按钮的宽度是视图宽度的三分之一)

您可以检查此answer

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40734110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档