UICollectionViewCell Frontera / Sombra


Al crear una aplicación para iPad, ¿cómo se puede dibujar un borde alrededor de un UICollectionViewCell?

Más detalles: Implementé una clase ProductCell que extiende UICollectionViewCell. Ahora, me gustaría asignar algunos detalles de lujo, por ejemplo, un borde, sombra, etc. Sin embargo, al intentar usar algo como esto aquí, Xcode me dice que el tipo de receptor 'CALayer' es una declaración forward.

Author: Community, 2012-10-29

4 answers

Solo por un poco más de implementación:

#import <QuartzCore/QuartzCore.h>

En su.m

Asegúrese de que su clase implementa

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

Ya que aquí es donde se configura la celda.

Luego puede cambiar cell.layer.background (solo disponible una vez que se importa el cuarzo)

Véase más adelante

- (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;
}
 71
Author: James Dawson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-26 20:36:19

Swift

Actualizado para Swift 3

Asumiendo que tiene su Vista de colección configurada con los métodos requeridos, solo puede escribir unas pocas líneas de código para agregar el borde.

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
}

Notas

  • no es necesario importar QuartzCore en Swift si ya ha importado UIKit.
  • Si también desea agregar sombra, vea esta respuesta.
 14
Author: Suragch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:47:05

Necesitas incluir el framework QuartzCore e importar el encabezado a tu clase:

#import <QuartzCore/QuartzCore.h>
 7
Author: Mundi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-29 12:16:09

Swift 4

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

Agréguelo en el método datasource, después de crear la celda

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
}
 1
Author: Naishta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-10 05:06:04