2012-06-27 9 views

risposta

42

Qualcosa di simile?

UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myImageView.frame.size.width, myImageView.frame.size.height/2)]; 
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]]; 
[myImageView addSubview:overlay]; 
+0

non credo che si suppone di aggiungere subviews a UIImageView. –

+0

@ HermanJ.RadtkeIII perché lo dici? –

+0

Questo [commento] (http://stackoverflow.com/questions/4892770/why-cant-you-add-a-subview-to-a-uiimageview-that-is-the-view-outlet-of-a- uiview # comment21186181_4892770) lo dice meglio. –

4

Hai pensato di aggiungere uno UIView con sfondo neroColor e 0.3 alfa come sottoview alla visualizzazione immagine?

4

Questo è simile alla risposta di MDT tranne utilizzando le proprietà CALayer:

UIView *blackOverlay = [[UIView alloc] initWithFrame: imageView.frame]; 
blackOverlay.layer.backgroundColor = [[UIColor blackColor] CGColor]; 
blackOverlay.layer.opacity = 0.3f; 
[self.view addSubview: blackOverlay]; 
+3

Uh oh, hai fatto un boo boo. Il colore di sfondo di CALayer è un CGColor, non un UIColor ^^ ;. – borrrden

8

La risposta MDT è corretta. Questo è solo un altro modo di utilizzare uno CAGradientLayer accanto a UIView. Penso che farà ciò che vuoi con più opzioni grafiche.

prima si dovrebbe aggiungere

#import <QuartzCore/QuartzCore.h> 

al vostro ViewController.m e qualsiasi luogo che si desidera aggiungere la sovrimpressione alla UIImage uso:

CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 
gradientLayer.frame = myImageView.layer.bounds; 

gradientLayer.colors = [NSArray arrayWithObjects: 
         (id)[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor, 
         (id)[UIColor colorWithWhite:0.0f alpha:0.3f].CGColor, 
         nil]; 

gradientLayer.locations = [NSArray arrayWithObjects: 
          [NSNumber numberWithFloat:0.0f], 
          [NSNumber numberWithFloat:0.5f], 
          nil]; 

//If you want to have a border for this layer also 
gradientLayer.borderColor = [UIColor colorWithWhite:1.0f alpha:1.0f].CGColor; 
gradientLayer.borderWidth = 1; 
[myImageView.layer addSublayer:gradientLayer]; 

Spero che questo vi aiuterà a fare lo

+0

potresti elaborarlo? –

11

Grazie a Mick MacCallum

Swift 3 Versione

let overlay: UIView = UIView(frame: CGRect(x: 0, y: 0, width: cell.imageView.frame.size.width, height: cell.imageView.frame.size.height)) 
overlay.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.1) 
cell.imageView.addSubview(overlay) 

ho usato per Swift 2,2

let overlay: UIView = UIView(frame: CGRectMake(0, 0, cell.imageView.frame.size.width, cell.imageView.frame.size.height)) 
overlay.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.1) 
cell.imageView.addSubview(overlay)