【问题标题】:How to change the size of a UICollectionView as a factor of screen size?如何将 UICollectionView 的大小更改为屏幕大小的一个因素?
【发布时间】:2017-07-12 03:44:03
【问题描述】:

UICollectionView 嵌入在 UIViewController 中。要求是让UICollectionView 的高度始终等于屏幕尺寸的一半。到目前为止已经尝试过的是:

override func viewWillAppear(_ animated: Bool) {
  self.caCollectionView.frame.size.width = self.view.frame.size.width
  self.caCollectionView.frame.size.height = self.view.frame.size.height / 2
}

这在构建和运行时似乎没有生效。 有什么遗漏吗? 请帮忙。提前谢谢你。

添加的屏幕截图:粉色区域为UIViewController,白色区域为UICollectionView

【问题讨论】:

  • 显示当前输出/截图。
  • @Imad 请查看更新后的输出/屏幕截图。感谢您的回复。
  • 你使用约束吗?
  • @anhtu 没有使用任何故事板约束。谢谢。

标签: ios swift uicollectionview autolayout


【解决方案1】:

你也向上移动底视图吗?

override func viewWillAppear(_ animated: Bool) {
  self.caCollectionView.frame.size.width = self.view.frame.size.width
  self.caCollectionView.frame.size.height = self.view.frame.size.height / 2
  self.pickAnotherView.frame.origin.y = self.caCollectionView.frame.origin.y+self.caCollectionView.frame.size.height
  self.pickAnotherView.frame.height = self.view.frame.height-self.caCollectionView.frame.size.height
}

您可以在带有约束的情节提要上执行此操作。

  1. 设置等高
  2. 将乘数设置为 0.5(视图高度的一半)

【讨论】:

  • 非常乐意帮助您。 :)
【解决方案2】:

始终在 viewDidLayoutSubviews 中更新 Frame 并使用 bounds 而不是 frame。

喜欢

override func viewDidLayoutSubviews()
  {

        super.viewDidLayoutSubviews()
        self.caCollectionView.frame.size.width = self.view.bounds.size.width 
        self.caCollectionView.frame.size.height = self.view.bounds.size.height / 2
  }

【讨论】:

  • @Jaydeep Vyas 感谢您的回复。我尝试根据您的代码进行更新,但仍然得到相同的输出。请参考我得到的输出截图。已在原帖中更新。
  • @asdiu 我认为您的图像数量较少尝试使用更多图像,而对于较少数量的图像,您必须增加 collectionview 单元格大小..
  • 一定会尝试的。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多