自动布局约束无法正常工作swift 3.0

时间:2017-07-07 12:08:11

标签: ios swift autolayout nslayoutconstraint

有点新的整个汽车布局约束部分。我的应用程序正在关闭测试版,我无法正确地获得约束。在下图中,我成功地为每个对象添加了以下约束

  • 水平放入容器
  • 所有物体都被固定在上 - 下 - 左 - 右到最近的物体。

正如你在图片中看到的那样,这种行为导致了Iphone 6和更大尺寸的精美布局,但是当在Iphone 5模拟器或Iphone 4预览上运行时,布局很糟糕。

Iphone 7 - 7plus and Iphone 4s

Iphone 5 simulator - Notice the labels are cropped and so is the img

如何在较小的设备上实现相同的布局行为,以便我的标签和img不会被裁剪?

提前谢谢!

2 个答案:

答案 0 :(得分:1)

您需要做的就是让您的标签与您的视图控制器具有相同的宽度,然后确保文本显示在中心,如下所示

enter image description here

通过这种方式,您可以确保文本可以居中并尽可能长。如果在这种情况下您的标签仍然无法完全显示,则需要缩小它。喜欢打电话

require(stringr)

k <- c("6 to 12 months",    
       "12 to 24 months",    
       "36 to 60 months",   
       "60 to 96 months",  
       "0 to 6 months", 
       "24 to 36 months",
       "96 to 120 months",
       "120 months & above")

index <- sapply(seq_along(k), function(x) stringr::str_split(k, pattern = "\\s")[[x]][1])

df <- data.frame(k, index = as.numeric(index))

df[order(index), ]

答案 1 :(得分:1)

对于每个标签,保持水平中心约束,但删除前导和尾随(左和右)约束。然后标签将自动调整大小以适应内容的宽度。

相关问题