2012-01-03 5 views
5

Ho uno storyboard che carica un UIView personalizzato. Inoltre, viene aggiunta una vista secondaria alla vista nello storyboard. Ha funzionato bene fino a quando non ho sovrascritto il metodo drawRect della vista secondaria, quindi ho visto un rettangolo nero invece della sottoview. Ecco il codice:Subview iOS visualizzato come un rettangolo nero se drawRect viene sovrascritto

#import <UIKit/UIKit.h> 
#import "MySubview.h" 

@interface MyView : UIView 

@end 

#import "MyView.h" 

@implementation MyView 

- (void) awakeFromNib 
{ 
    CGRect frame = [self frame]; 
    MySubview* sv = [[MySubview alloc] initWithFrame:frame]; 
    [self addSubview:sv]; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

@end 

#import <UIKit/UIKit.h> 

@interface MySubview : UIView 

@property (retain, nonatomic) NSString* text; 
@property (retain, nonatomic) UILabel* label; 

@end 

#import "MySubview.h" 

@implementation MySubview 

@synthesize text, label; 


- (void)attachLabel 
{ 
    text = @"Hello"; 
    label = [[UILabel alloc] init]; 
    [label setText:text]; 
    [label setFont:[UIFont fontWithName:@"Futura" size:18]]; 
    [label setBackgroundColor:[UIColor clearColor]]; 

    [label sizeToFit]; 

    CGRect labelFrame = label.frame; 
    labelFrame.origin.x = (self.frame.size.width - labelFrame.size.width)/2; 
    labelFrame.origin.y = (self.frame.size.height - labelFrame.size.height)/2; 
    label.frame = labelFrame; 

    [self addSubview:label]; 
} 

- (void)awakeFromNib 
{ 
    [self attachLabel]; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self attachLabel]; 
    } 
    return self; 
} 

// Works if I comment this out! 
- (void)drawRect:(CGRect)rect 
{ 
} 

@end 

Aggiornamento - Aggiunto codice disegno sottostante:

- (void)drawRectWithRoundBorders:(CGRect)rect 
{ 
    [super drawRect:rect]; 

    // Parameters used for drawing. 
    const CGFloat lineWidth = 5; 
    const CGFloat shadowOffset = 3; 
    const CGFloat shadowBlur = 4; 
    const CGFloat spaceToBB = 10; // Space to the bounding box of this view. 
    const CGFloat cornerRadii = 5; 
    const CGFloat lineColor[4] = { 0, 0, 0, 1 }; 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(ctx, lineWidth); 
    CGContextSetStrokeColor(ctx, lineColor); 
    CGContextSetShadow(ctx, CGSizeMake(shadowOffset, shadowOffset), shadowBlur); 

    CGRect innerRect = rect; 

    innerRect.size.width -= 2*spaceToBB; 
    innerRect.size.height -= 2*spaceToBB; 
    innerRect.origin.x += spaceToBB; 
    innerRect.origin.y += spaceToBB; 

    UIBezierPath *path = 
    [UIBezierPath bezierPathWithRoundedRect:innerRect 
          byRoundingCorners:UIRectCornerAllCorners 
           cornerRadii:CGSizeMake(cornerRadii, cornerRadii) 
    ]; 

    CGContextAddPath(ctx, path.CGPath); 
    CGContextStrokePath(ctx); 
} 


- (void)drawRect:(CGRect)rect 
{ 
    [self drawRectWithRoundBorders:rect]; 
} 

Aggiornamento

Sembra funzionare quando riempio il rettangolo di selezione della vista sub con un po 'di colore prima.

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGFloat white[] = {1, 1, 1, 0.5}; 
CGContextSetFillColor(ctx, white); 
CGContextAddRect(ctx, rect); 
CGContextFillPath(ctx); 
+0

Quando fa questo '- (void) drawRectWithRoundBorders: (CGRect) rect' viene chiamato? – jrturton

+0

@jrturton in drawRect, basta scorrere verso il basso un bit – Nils

+0

Oh, giusto, ero confuso dall'implementazione vuota sopra - dovresti probabilmente rimuoverlo dalla domanda (e dal tuo codice ??) – jrturton

risposta

39

Basta aggiungere [self setOpaque:NO] nel metodo initWithFrame:.

4

Questo perché il tuo drawRect non sta facendo nulla. Hai la precedenza su drawRect se vuoi fare un disegno personalizzato. Quindi:

  • Se non si desidera eseguire il disegno personalizzato, non eseguire l'override di drawRect.
  • Se si desidera eseguire un disegno personalizzato, effettuare effettivamente un'operazione in drawRect.
+0

Hummm ma se carico la classe subview direttamente in uno storyboard funziona. Anche se disegno qualcosa in drawRect è ancora nero. – Nils

+0

o aggiungi '[super drawRect: rect]' se vuoi disegnare ontop della vista predefinita :) – deanWombourne

+0

Qual è il tuo codice di disegno per disegnare qualcosa? Se è ancora nero, lo stai disegnando in modo errato. – mattjgalloway

Problemi correlati