UIcollectonView问题中的UIImage



我正试图使用以下代码将UIImage添加到UICollectionViewController中:

let backgroundImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 430, height: 550))
backgroundImage.image = UIImage(named: "fantasy_football_hero_tile_cropped.png")
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFit
backgroundImage.clipsToBounds = true
self.view.insertSubview(backgroundImage, at: 0)

我的问题是图像不在屏幕的底部/最低部分,这正是我试图实现的。。。我怎样才能把它夹到底?!我没有使用故事板,我可以像在故事板中那样以编程方式将图像锚定到底部吗?

这一点尤其正确,因为手机的屏幕尺寸不同,但人们仍然希望将其固定在底部。

您可以通过程序进行添加。我认为你在self.view中添加/插入了一些视图,这也会影响简单的场景

let backgroundImageV = UIView(frame: CGRect(x: 0, y: 0, width: 430, height: 500))
backgroundImageV.backgroundColor =  UIColor.blue
backgroundImageV.contentMode = UIView.ContentMode.scaleAspectFit
backgroundImageV.clipsToBounds = true
self.view.addSubview(backgroundImageV)

let backgroundImageV2 = UIView(frame: CGRect(x: 0, y: 0, width: 430, height: 510))
backgroundImageV2.backgroundColor =  UIColor.green
backgroundImageV2.contentMode = UIView.ContentMode.scaleAspectFit
backgroundImageV2.clipsToBounds = true
self.view.insertSubview(backgroundImageV2, at: 0)
let backgroundImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 430, height: 550))
backgroundImage.backgroundColor =  UIColor.red
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFit
backgroundImage.clipsToBounds = true
self.view.insertSubview(backgroundImage, at: 0)

红色将在底部绿色将在1。因为您稍后会添加红色视图。所以它会起作用的。你能出示你的完整代码吗。它将工作

这里的一个问题是UICollectionViewController的self.view是UICollectionView。这是一个滚动视图,所以无论你把这个图像视图放在哪里,当滚动视图滚动时,它都会移动。

如果这不是您想要的,那么有一个简单的解决方案:将图像视图设置为集合视图的backgroundView。这是一个静止的视图,无论如何滚动,它都会形成集合视图的背景。如果将图像视图的contentMode设置为bottom,则图像将位于底部的中心,这似乎就是您想要的。

最新更新