2010-11-30 19 views
94

Sono un po 'una perdita. Ho usato la proprietà layer di UIView per arrotondare gli angoli di più elementi nella mia app. Tuttavia, questo UIImageView semplicemente non sta rispettando. Non sono sicuro di ciò che mi manca.Impostazione raggio angolo su UIImageView non funzionante

UIImageView (denominato previewImage) è contenuto in una cella di visualizzazione tabella. Ho provato a impostare la proprietà cornerRadius posizione multipla (nella cella stessa e nel controller che crea la cella) senza alcun risultato.

static NSString *CellIdentifier = @"MyTableViewCell"; 

MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]; 
    cell = [topLevelObjects objectAtIndex:0]; 
    cell.previewImage.layer.cornerRadius = 20; //Made it 20 to make sure it's obvious. 
} 

C'è qualcosa nel modo in cui vengono caricate le celle che mi mancano?

risposta

317

È necessario impostare la proprietà del livello masksToBounds-YES:

cell.previewImage.layer.masksToBounds = YES;

Questo è perché il controllo UIImageView crea una pseudo-visualizzazione secondaria per contenere l'oggetto UIImage.

+12

Attenzione questo sarà un grande successo nelle prestazioni – jjxtra

+5

Perché è un successo nelle prestazioni? – Michael

+0

@PsychoDad Perché? –

11

credo è necessario impostare:

cell.previewImage.layer.masksToBounds = YES; 
cell.previewImage.layer.opaque = NO; 
18

Questo dovrebbe funzionare

cell.previewImage.clipsToBounds = YES; 

cell.previewImage.layer.cornerRadius = 20; 
+2

Non sono sicuro che 'clipsToBounds' sia un metodo della classe successiva. Credi che sia 'masksToBounds' come sopra. –

+3

I livelli @AlfieHanssen hanno maschereToBound :, le visualizzazioni hanno clipToBounds: – Kevin

3

in Xcode Interface Builder, selezionando l'attributo Disegno 'subviews clip' per la vista insieme con l'impostazione del raggio d'angolo a il codice cell.previewImage.layer.cornerRadius = 20; fa il lavoro per me!

See 'Clip Subviews' option in IB

2

Prova questo sotto pezzo di codice

cell.previewImage.layer.cornerRadius = 20; 
cell.previewImage.clipsToBounds = YES; 
6

Vale anche la pena notare che

  1. Se si utilizza aspectFit E cornerRadius con clipsToBounds/masksToBounds, non sarà possibile ottenere il angoli arrotondati.

cioè se avete questo

theImageView.contentMode = .scaleAspectFit 

e

theImageView.layer.cornerRadius = (theImageView.frame.size.height)/2 
    theImageView.clipsToBounds = true 

o

theImageView.layer.masksToBounds = true 

E non funziona. si dovrà sbarazzarsi di codice aspectFit

//theImageView.contentMode = .scaleAspectFit 
  1. Assicurarsi che la larghezza e l'altezza per il Punto di vista è lo stesso
+0

Questo forse corretto – onmyway133

+0

Sul maledetto punto! –

+0

2 ore giù per lo scarico, buona cattura :) – alasker

0
-(void) viewDidAppear:(BOOL)animated{ 
     [super viewDidAppear:animated]; 
     [self setMaskTo:viewDistance 
      byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight]; 
      } 

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners 
    { 
     UIBezierPath *rounded = [UIBezierPath 
       bezierPathWithRoundedRect:view.bounds 
               byRoundingCorners:corners 

        cornerRadii:CGSizeMake(20.0, 20.0)]; 

      CAShapeLayer *shape = [[CAShapeLayer alloc] init]; 
      shape.frame = self.view.bounds; 
     [shape setPath:rounded.CGPath]; 
      view.layer.mask = shape; 
     } 
Problemi correlati