【问题标题】:UICollectionViewCell Border / ShadowUICollectionViewCell 边框/阴影
【发布时间】:2012-10-29 12:05:29
【问题描述】:

在构建 iPad App 时,如何在 UICollectionViewCell 周围绘制边框?

更多细节:我实现了一个扩展 UICollectionViewCell 的类 ProductCell。现在,我想分配一些花哨的细节,例如边框、阴影等。但是,当尝试使用 this here 之类的东西时,Xcode 告诉我接收器类型 'CALayer' 是前向声明。

【问题讨论】:

    标签: objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

    只是为了更多的实现:

    #import <QuartzCore/QuartzCore.h>
    

    在你的.m中

    确保你的类实现了

    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
    

    因为这是设置单元的位置。

    然后您可以更改cell.layer.background(仅在导入石英后可用)

    见下文

    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        MyCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"pressieCell" forIndexPath:indexPath];
        //other cell setup here
    
        cell.layer.borderWidth=1.0f;
        cell.layer.borderColor=[UIColor blueColor].CGColor;
    
        return cell;
    }
    

    【讨论】:

      【解决方案2】:

      斯威夫特

      为 Swift 3 更新

      假设您有Collection View set up with the required methods,您只需编写几行代码即可添加边框。

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
          cell.myLabel.text = self.items[indexPath.item]
          cell.backgroundColor = UIColor.cyan 
          
          // add a border
          cell.layer.borderColor = UIColor.black.cgColor
          cell.layer.borderWidth = 1
          cell.layer.cornerRadius = 8 // optional
          
          return cell
      }
      

      备注

      • 如果您已经导入了UIKit,则无需在Swift 中导入QuartzCore
      • 如果您还想添加阴影,请参阅this answer

      【讨论】:

        【解决方案3】:

        您需要包含框架QuartzCore 并将标头导入您的类:

        #import <QuartzCore/QuartzCore.h>
        

        【讨论】:

          【解决方案4】:

          斯威夫特 4

          cell.layer.borderColor = UIColor.black.cgColor
          cell.layer.borderWidth = 1
          

          在数据源方法中添加它,创建单元格后

          func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
               let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath)
               cell.layer.borderColor = UIColor.black.cgColor
               cell.layer.borderWidth = 1
          }
          

          【讨论】:

            【解决方案5】:

            我认为最好将此配置添加到您的自定义单元实现中,而不是在数据源委托方法中。

            cell.layer.borderColor = UIColor.black.cgColor
            cell.layer.borderWidth = 1
            cell.layer.cornerRadius = 8 // optional
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-09-18
              • 1970-01-01
              • 1970-01-01
              • 2018-08-29
              • 1970-01-01
              • 1970-01-01
              • 2020-07-09
              • 2021-11-27
              相关资源
              最近更新 更多