【问题标题】:how to give separate height and width constraint for specified screen size for eg giving different constraints for iphone and ipad?如何为指定的屏幕尺寸提供单独的高度和宽度约束,例如为 iphone 和 ipad 提供不同的约束?
【发布时间】:2018-02-27 09:35:34
【问题描述】:
let cell = tableview.dequeueReusableCell(withIdentifier: "cell1") as! viewTableViewCell
let head = titlelist[indexPath.row] as! String
cell.lbl.text = head.uppercased()    
if(UIScreen.main.bounds.size.height == 1366)
{
    cell.img.heightAnchor.constraint(equalToConstant: 216).isActive = true
    cell.img.widthAnchor.constraint(equalToConstant: 395).isActive = true
    self.view.updateConstraints()
}

我想在单元格中更改我的 imageview img 的宽度和高度限制。但它没有更新。感谢任何帮助。

【问题讨论】:

  • 您是在单独的Xib 中设计此单元还是在Storyboard 中作为原型单元?
  • 我在 Storboard 中做到了
  • 您是否以编程方式在单元格中添加 imageView?或者只是使用 Drag & Drop 添加 imageView ?
  • 图像视图使用拖放并给它一个固定的高度和宽度。这适用于iphone。我正在尝试通过代码为 ipad 提供新的高度和宽度限制
  • 您也可以使用情节提要。

标签: ios autolayout swift4 xcode9


【解决方案1】:

你可以试试

cell.img.translatesAutoresizingMaskIntoConstraints = false

if UIDevice.current.userInterfaceIdiom == .pad {
    //iPad
    cell.img.heightAnchor.constraint(equalToConstant: 400).isActive = true
    cell.img.widthAnchor.constraint(equalToConstant: 500).isActive = true
} 
else if UIDevice.current.userInterfaceIdiom == .phone {
    //iPhone
    cell.img.heightAnchor.constraint(equalToConstant: 216).isActive = true
    cell.img.widthAnchor.constraint(equalToConstant: 395).isActive = true
} 

cell.img.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 5).isActive = true
cell.img.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: 5).isActive = true
cell.img.centerXAnchor.constraint(equalTo: cell.contentView.centerXAnchor, constant: 5).isActive = true

这需要动态表格高度

【讨论】:

  • 偏移值应该是多少。编译器对我大喊“未解决的标识符偏移”
  • 它是填充替换它任何数字,编辑中没有偏移
  • 如何给动态表格高度
【解决方案2】:

就像下面的图片。选择您的约束让我们说“宽度”,然后选择查看左侧打开的面板,您会在 constant 之前看到一个 + 按钮,只需点击它,就会打开一个弹出窗口,其中包含类似的菜单

  • 宽度:紧凑
  • 高度:常规
  • 宽度:任意

这适用于 iPhone。要为 iPad 创建另一个常量,只需将宽度形式 Compact 更改为 Regular

现在您的约束有两个常量变量。一款用于 iPhone,一款用于 iPad。根据您的要求分配不同的常量值。

PS:你不应该从+改变高度和宽度是不同的 事物。 Compact Width and Regular Height 表示 iPhoneRegular Width and Regular Height 表示iPad

【讨论】:

猜你喜欢
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
相关资源
最近更新 更多