2012-01-03 60 views
5

我有一個加載加載自定義UIView的故事板。還有一個子視圖被添加到故事板中的視圖。它工作得很好,直到我覆蓋了子視圖的drawRect方法,然後我只看到了一個黑色的矩形而不是子視圖。下面是代碼:如果drawRect被覆蓋,iOS子視圖顯示爲黑色矩形

#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 

更新 - 增加了以下繪圖代碼:

- (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]; 
} 

更新

看來,當我填寫子視圖的邊框有一些顏色先工作。

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

什麼時候調用' - (void)drawRectWithRoundBorders:(CGRect)rect? – jrturton 2012-01-03 15:43:05

+0

@jrturton在drawRect中,向下滾動一下 – Nils 2012-01-03 17:52:45

+0

哦,對了,我被上面的空實現弄糊塗了 - 你應該把它從問題(和你的代碼??)中移除 – jrturton 2012-01-03 18:48:14

回答

39

只需在您的initWithFrame:方法中添加[self setOpaque:NO]即可。

4

那是因爲你的drawRect是什麼都不做。如果您想進行自定義繪圖,則可以覆蓋drawRect。所以:

  • 如果你不想做自定義繪製則不會覆蓋drawRect
  • 如果你確實想做自定義繪圖,那麼實際上在drawRect中做一些事情。
+0

嗡嗡聲,但如果我直接在故事板中加載子視圖類它的作品。另外,如果我在drawRect中繪製某些東西,它仍然是黑色的。 – Nils 2012-01-03 11:29:23

+0

或者如果你想繪製默認視圖的ontop,你可以添加'[super drawRect:rect]':) – deanWombourne 2012-01-03 11:29:45

+0

什麼是你的繪圖代碼來繪製一些東西?如果它仍然是黑色的,那麼你錯誤地繪製它。 – mattjgalloway 2012-01-03 11:34:15

相關問題