OGeek|极客世界-中国程序员成长平台

标题: ios - 不同设备宽度的不同尺寸和 View 对齐方式 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:45
标题: ios - 不同设备宽度的不同尺寸和 View 对齐方式

如何在 iPhone 6 纵向模式和横向模式下实现 View 跨越设备宽度,最大宽度为 400 像素并与右侧对齐?

这可以通过使用设备类和/或改变特征来实现吗?我真正想要的是 View 适应设备的宽度:如果它比 400 像素宽(因为我将设备旋转到横向模式或者因为应用程序在 iPhone Plus 或 iPad 上运行)它不应该再跨越显示器的宽度,但最大宽度为 400 像素并位于右侧。也许这只能通过代码来完成?

感谢您的任何意见。



Best Answer-推荐答案


What I really want, is that the view adapts to the width of the device: if it is wider than 400px (because I rotated the device to landscape mode or because the app runs on an iPhone Plus or iPad) it should no longer span the width of the display but rather have a maximum width of 400px and sit on the right side.

这可以通过设置以下约束来控制 View 的宽度和位置来实现。

  1. 使用常量 0 和优先级 1000 将 View 固定到其父 View 的后沿。
  2. 使用常量0和优先级750将 View 固定到其父 View 的前沿。
  3. 为 View 设置宽度约束,使其小于或等于400,优先级为1000

在 iPhone 6 上以纵向查看时,所有三个约束都可以轻松满足。

当在 iPhone 6 上以横向查看时,约束 2(具有较低优先级)被打破以满足其他 2 个约束。 Auto Layout 将尽最大努力满足约束 2,并在不破坏 400 单位的宽度约束的情况下使 View 尽可能宽。

您的 View 需要其他约束来确定其高度和垂直位置。


constraints in Storyboard


gif showing running in the simulator

关于ios - 不同设备宽度的不同尺寸和 View 对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41696377/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4