【问题标题】:Button image sizes not changing in iPad by autolayout using iOS?使用 iOS 的自动布局不会在 iPad 中更改按钮图像大小?
【发布时间】:2016-03-03 21:40:44
【问题描述】:

我正在尝试在 iPhone 和 iPad 底部中心创建按钮图像,但按钮大小不会改变 iPad。它仅显示 iPhone 尺寸。请帮助我创建我的场景。我正在为我的通用应用程序使用不同的按钮图像尺寸。

【问题讨论】:

  • 你只希望这个按钮在视图的中心吗?
  • 不仅如此。那是按钮图像。我想根据设备尺寸 ipad 和 iphones@Jaimish 调整大小
  • @android 如果您调整按钮的大小,图像将自动调整大小...
  • 你把按钮放在底部居中现在你想根据设备调整大小我是对的吗?
  • @android 我已经发布了我的答案,你可以验证它......

标签: ios iphone xcode storyboard


【解决方案1】:
  1. 首先为按钮指定纵横比,以便根据设备大小,它会按照给定的比例增加按钮的高度和宽度。按照屏幕截图给出纵横比。

  1. 现在要使视图中心的按钮在容器中水平居中,请参见屏幕截图。

【讨论】:

  • 它降到了底部,但仍未调整大小。在 iPad 上显示 iPhone 尺寸。@Jaimish
  • iphone 上面我已经发布了相同尺寸的 iPad 也显示。按钮尺寸上方的 iPhone 屏幕看起来不错,但对于 iPad,小按钮尺寸看起来不太好。我在资产中拥有不同的图像尺寸,但所有设备的按钮尺寸都相同。@Jaimish
【解决方案2】:

以上图为例:

1.我知道您已经完成了水平居中以查看约束。

2.向视图添加前导约束。

3.添加尾随约束到视图。

4.添加底部空间约束到视图。

5.暂时不要设置任何高度和宽度。

现在只需运行它,您就会得到确切的要求......

希望对你有所帮助..

【讨论】:

  • 请发布一些屏幕截图。我是自动布局的新开发者。@Kishore Kumar
  • @android 没有问题我会发布
【解决方案3】:

您必须基于percentage(%) 添加约束,以便按钮图像可以根据device(iPhone/iPad) screen size 而变化。

按钮上 StoryBoard 的约束

Step 1. Drag button and place it on its proper position.

Step 2. Now Add constraint Horizontal Center in Container.

Step 2. Now Add Bottom Pin constraint.

Step 3. Add constraint Equal Width and Equal Height.

Step 4. Now update multiplier of Equal Width constraint and Equal Height.

使用KVConstraintExtensionsMaster 以编程方式更新 iPad 上的约束。

- (void)viewDidLoad
{
    [super viewDidLoad];

    /* preparing View Hierarchy */
    UIButton *button = [UIButton prepareNewViewForAutoLayout];
    [self.view addSubview:button];

    /* applying constraints */
    [button applyConstraintForHorizontallyCenterInSuperview];
    [button applyBottomPinConstraintToSuperviewWithPadding:defualtConstant];

    [button applyWidthConstraint:280]; // fixed width for iphone
    [button applyHeightConstrain:40]; // fixed height for iphone

    // update width constraint with proper iPad ratio
    [button updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeWidth];

    // update height constraint with proper iPad ratio
    [button updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeHeight];
    /*
     // update Height constraints at any specific ratio on iPad
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [button updateAppliedConstraintConstantValueByAttribute:NSLayoutAttributeHeight withConstantRatio:1.8];
     }
     */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多