Ios UICollectionView椭圆形自定义单元
如何在Ios UICollectionView椭圆形自定义单元,ios,iphone,swift,uicollectionview,Ios,Iphone,Swift,Uicollectionview,如何在UICollectionView中使用自定义单元格生成椭圆形单元格。下面是我试图实现的图像 不知道该如何实现这一点,通过几个环节,但没有得到。 请导游。 谢谢 更新:我尝试的是 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -&g
UICollectionView
中使用自定义单元格生成椭圆形单元格。下面是我试图实现的图像
不知道该如何实现这一点,通过几个环节,但没有得到。
请导游。
谢谢
更新:我尝试的是
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/3.0 - 8, height: collectionView.frame.size.width/2.5[![enter image description here][2]][2] )
}
对于此设计,但它不能按要求工作,因此
将imageview/button(您的选择)添加到集合视图单元格中,边框与整个单元格一样大,使单元格背景清晰,为按钮添加您想要的颜色,并将按钮的layer.cornerradius设置为您想要的椭圆(即按钮高度的1/2是精确的圆形)。获取单元格对象
在contentView上添加子视图(容器视图)
设置容器的边界半径(例如2)
在容器中添加其他UI元素。如果您试图在UIcollectionview中创建一个单元格,请使用圆角 那么你可以试试这个
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:CellCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath)as! CellCollectionViewCell
cell.backgroundColor=UIColor.blueColor()
cell.layer.cornerRadius=26 //try with different values untill u get the rounded corners
cell.layer.masksToBounds=true
cell.cellLabel.text=label[indexPath.row]
return cell
}
TL;DR:(示例可在此处找到:)
唷,这个视图有三个问题:
UICollectionView
将文本作为单元格长度的单元格if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSizeMake(1, 1)
}
正确的方法是,例如viewDidLoad
func。当然,如果以后要覆盖布局,请记住此if
然后,对于要自行调整大小的标签,需要设置:
cell.titleLabel.preferredMaxLayoutWidth = 50
在这两个示例中使用的值并不重要。如果将estimatedItemSize设置为较低,则基本上要求自调整大小
2)
为此,需要重写布局类。我强烈建议使用@Alex Koshy answer提供的此解决方案
3)
这部分很简单。只需在自定义单元格的底部添加子视图,并将单元格视图背景色设置为透明即可。并在单元格中添加角半径,如下所示:
override func prepareForReuse() {
self.roundedView.layer.cornerRadius = self.frame.size.height/2
}
下面是一个输出效果:
下面是指向存储库的链接,示例如下:
享受吧 26对于适当的椭圆形是可以的,但不能在一行中获得3个单元格-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout SizeFormiteIndeXPath:(NSIndexPath*)indexPath;在这个方法中,返回collectionview.width/3…它将在一行中提供3个单元格。仅当标签文本大小固定时才执行此操作,如果文本大小增加得更多,该怎么办。如果您希望一行有三个单元格,请转到主单元格。情节提要在viewController中选择collectionView在选择collectionView后,转到“属性”窗格中“大小检查器”“设置调用和行的最小单元格间距”中的“大小检查器”。根据您的要求调整值希望这可能会解决您的问题那么您的问题到底是什么,只是项目的大小?这帮了我很大的忙,谢谢,但是有些问题并不完全是需要的,正在处理它们。您能告诉我如何在选择单元格时更改roundedView的颜色吗,我尝试了DidSelectMethod,但不适用于我。只需覆盖单元格中的
setSelected:
方法没有得到您,另一个问题是单元格之间的间距将其设置为10,但不希望显示一行中的三个单元格。在6和6+上,显示三行,但间距远大于10,在5上,一行中只有两个单元格。func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,InsertForSectionAtIndex节:Int)->UIEdgeInsets{let marginStaticValue:CGFloat=10返回UIEdgeInsetsMake(0,marginStaticValue,0,marginStaticValue)}