当手机进入横向时,以编程方式更改自定义表格单元格中标签的约束。 ios swift

时间:2022-08-05 22:23:22

I have a label in a custom cell and it currently has a width constraint of 150. When the phone goes into landscape orientation, I would like to increase the width constraint to 300. Then of course change it back to 150 when phone goes back to portrait. How could I accomplish this?

我在自定义单元格中有一个标签,它目前的宽度约束为150.当手机进入横向时,我想将宽度约束增加到300.然后当电话返回时,将其更改回150肖像。我怎么能做到这一点?

1 个解决方案

#1


0  

You can combine Size Classes to achieve this. You can set up 300 on AnyWidth CompactHeight(landscape mode for iPhone) and set up 150 on CompactWidth RegularHeight(portrait mode for iPhone).

您可以组合大小类来实现此目的。您可以在AnyWidth CompactHeight(iPhone的横向模式)上设置300并在CompactWidth RegularHeight(iPhone的纵向模式)上设置150。

Enable Size Classes in the Interface Builder Document. Then, you will need to configure based on on different size classes. More information, you can find it online: Adaptive Layout from Ray Tutorial

在Interface Builder文档中启用大小类。然后,您需要根据不同的大小类进行配置。更多信息,您可以在线找到它:Ray Tutorial中的Adaptive Layout

Another approach will be: changing the constraint's constant value based on different mode of the screen. You will need to reference the constraint as IBOutlet and then change it programatically. But, we don't suggest to do so because Apple is tended to have different size of device and it's better to work on layout by combining Auto Layout and Size Classes

另一种方法是:根据屏幕的不同模式更改约束的常量值。您需要将约束引用为IBOutlet,然后以编程方式更改它。但是,我们不建议这样做,因为Apple倾向于使用不同大小的设备,最好通过组合自动布局和大小类来处理布局

#1


0  

You can combine Size Classes to achieve this. You can set up 300 on AnyWidth CompactHeight(landscape mode for iPhone) and set up 150 on CompactWidth RegularHeight(portrait mode for iPhone).

您可以组合大小类来实现此目的。您可以在AnyWidth CompactHeight(iPhone的横向模式)上设置300并在CompactWidth RegularHeight(iPhone的纵向模式)上设置150。

Enable Size Classes in the Interface Builder Document. Then, you will need to configure based on on different size classes. More information, you can find it online: Adaptive Layout from Ray Tutorial

在Interface Builder文档中启用大小类。然后,您需要根据不同的大小类进行配置。更多信息,您可以在线找到它:Ray Tutorial中的Adaptive Layout

Another approach will be: changing the constraint's constant value based on different mode of the screen. You will need to reference the constraint as IBOutlet and then change it programatically. But, we don't suggest to do so because Apple is tended to have different size of device and it's better to work on layout by combining Auto Layout and Size Classes

另一种方法是:根据屏幕的不同模式更改约束的常量值。您需要将约束引用为IBOutlet,然后以编程方式更改它。但是,我们不建议这样做,因为Apple倾向于使用不同大小的设备,最好通过组合自动布局和大小类来处理布局