简体   繁体   English

如何在Xcode中将大小类应用于UIButton

[英]How to apply size classes to a UIButton in Xcode

I want my button to have a size of 30x35 on iPhone and 60x70 on iPad. 我希望按钮在iPhone上的尺寸为30x35 ,在iPad上的尺寸为60x70 I create the button in a storyboard. 我在情节提要中创建按钮。 I know what I need to use size classes. 我知道我需要使用尺寸等级。 But I can't create different sizes on different devices. 但是我无法在不同的设备上创建不同的尺寸。 I use this sequence of actions: 我使用以下动作序列:

  1. Select iPad Pro 9,7 in storyboard. 在情节提要中选择iPad Pro 9,7。

  2. Create button and change width 60 , height 70 . 创建按钮并更改宽度60 ,高度70

  3. Create width and aspects ratio constraints. 创建宽度和长宽比约束。

  4. In width constraint unmark Installed press plus and create wR hR Installed . 在宽度约束中,取消标记Installed然后按加号并创建wR hR Installed

  5. Select iPhone 8 in storyboard. 在情节提要中选择iPhone 8。

  6. Change width 30 , height 35 . 更改宽度30 ,高度35

  7. Create width constraints. 创建宽度约束。

Sometimes I create different sizes on iPhone and iPad but sometimes this doesn't work. 有时我在iPhone和iPad上创建不同的尺寸,但有时不起作用。

Is this the right sequence of actions or not? 这是正确的行动顺序吗?

Update 更新

Please tell me if I'm not right. 请告诉我我是否不对。 For my button in iPad (60x70) I should create width constraint and press "installed" wR hR. 对于iPad(60x70)中的按钮,我应该创建宽度限制,然后按“已安装” wR hR。 For my button in iPhone (30x35) I should create one more width constraint and press "installed" for wC hC, wR hC, wC hR? 对于我在iPhone(30x35)中的按钮,我应该再创建一个宽度约束,然后为wC hC,wR hC,wC hR按下“已安装”?

If you just want to vary the width for iphone and ipad, JUST apply size class to your UIButton. 如果您只是想更改iPhone和iPad的宽度, 只需将size类应用于UIButton。

Go to attributes inspector of your button and click plus button to the left of width property. 转到按钮的attributes inspector器,然后单击width属性左侧的加号按钮。

Selected Width: Regular and Height: Regular. 选定的宽度:常规和高度:常规。 and tap on button Add Variation 然后点击按钮Add Variation

在此处输入图片说明

Change the width for ipad (w R h R) 更改iPad的宽度(w R h R)

Now, i my case, width will be 200 for IPhone and 400 for IPad 现在,我的情况是, iPhone的宽度为200iPad的宽度为400

适用宽度

Note . 注意 You don't need to click on installed property even single time. 甚至不需要一次单击installed属性。

Do let me know if you need any help 如果需要帮助,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM