Ios 加载具有与其UICollectionView不同alpha值的UICollectionViewCell
我无法为我的Ios 加载具有与其UICollectionView不同alpha值的UICollectionViewCell,ios,iphone,swift,uicollectionview,uicollectionviewcell,Ios,Iphone,Swift,Uicollectionview,Uicollectionviewcell,我无法为我的UICollectionViewCell指定与拥有它们的UICollectionView不同的alpha。我看过其他一些类似的帖子,但没有一篇适合我。我有一个带有UICollectionView的.xib文件,其中alpha设置为0.9,其中的单元格从另一个.xib文件加载,alpha设置为1.0。无论我做了什么尝试,UICollectionView的alpha优先。这些尝试包括: func collectionView(collectionView: UICollectionVie
UICollectionViewCell
指定与拥有它们的UICollectionView
不同的alpha。我看过其他一些类似的帖子,但没有一篇适合我。我有一个带有UICollectionView
的.xib文件,其中alpha设置为0.9,其中的单元格从另一个.xib文件加载,alpha设置为1.0。无论我做了什么尝试,UICollectionView
的alpha优先。这些尝试包括:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! Cell
// Here is the first attempt
cell.alpha = 1.0
cell.contentView.alpha = 1.0
// Here is the second attempt
let attribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attribute.alpha = 1.0
cell.applyLayoutAttributes(attribute)
cell.setNeedsDisplay()
return cell
我希望单元格加载时没有透明度,但我希望包含它们的集合视图稍微透明。每个单元格上都加载了一个完全不透明的唯一图像
编辑1:我尝试将UICollectionView
的backgroundColor
设置为1.0 alpha清除,然后在我的viewDidLoad()
方法中给它一个自定义backgroundView
。我有一个集合视图的@IBOutlet
,用于设置以下内容:
@IBOutlet weak var collectionView: UICollectionView!
// MARK: -View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
let background = UIView(frame: collectionView.bounds)
background.backgroundColor = UIColor(red: 26/255.0, green: 26/255.0, blue: 26/255.0, alpha: 0.97)
collectionView.backgroundView = background
collectionView.registerNib(UINib(nibName: "Cell", bundle: nil), forCellWithReuseIdentifier: "Cell")
}
这确实有效。您不能使用alpha创建父视图!=1和alpa=1的孩子,他们不会像预期的那样工作
通常我所做的是使用一个辅助视图(与集合视图相同的框架),父视图(您的集合视图)的同级视图,alpha设置为X值。将集合视图背景颜色设置为clearColor。是否希望集合视图的alpha值为0.9,而集合视图单元格的alpha值为1.0?是的!collectionView的alpha值为0.9,它为单元格提供了相同的alpha值。请尝试将背景视图设置为collection视图,然后更改其alpha值。希望这将有助于请查看我的编辑。恐怕我做得不对吧?