2012-01-03 21 views
5

Tôi có bảng phân cảnh tải tải UIView tùy chỉnh. Chế độ xem phụ cũng được thêm vào chế độ xem trong bảng phân cảnh. Nó hoạt động tốt cho đến khi tôi ghi đè lên phương thức drawRect của khung nhìn phụ, sau đó tôi chỉ thấy một hình chữ nhật màu đen thay vì khung nhìn con. Đây là mã:Chế độ xem phụ iOS hiển thị dưới dạng hình chữ nhật màu đen nếu drawRect bị ghi đè

#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 

Cập nhật - Thêm bản vẽ mã bên dưới:

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

Cập nhật

Dường như với hoạt động khi tôi điền vào khung giới hạn của cái nhìn phụ với một số màu đầu tiên.

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

Khi nào điều này '- (void) drawRectWithRoundBorders: (CGRect) rect' được gọi? – jrturton

+0

@jrturton trong drawRect, chỉ cần cuộn xuống một chút – Nils

+0

Oh phải, tôi đã nhầm lẫn bởi việc thực hiện trống ở trên - bạn có lẽ nên loại bỏ điều đó khỏi câu hỏi (và mã của bạn ??) – jrturton

Trả lời

39

Chỉ cần thêm [self setOpaque:NO] vào phương pháp initWithFrame: của bạn.

4

Đó là vì drawRect của bạn không làm gì cả. Bạn ghi đè drawRect nếu bạn muốn vẽ tùy chỉnh. Vì vậy:

  • Nếu bạn không muốn vẽ tùy chỉnh thì đừng ghi đè drawRect.
  • Nếu bạn muốn làm bản vẽ tùy chỉnh thì thực sự làm điều gì đó trong drawRect.
+0

Hummm nhưng nếu tôi tải lớp phụ trực tiếp trong bảng phân cảnh thì nó hoạt động. Ngoài ra nếu tôi vẽ một cái gì đó trong drawRect nó vẫn còn màu đen. – Nils

+0

hoặc thêm '[siêu drawRect: rect]' nếu bạn muốn vẽ ontop của giao diện mặc định :) – deanWombourne

+0

Mã vẽ của bạn là gì? Nếu nó vẫn còn màu đen thì bạn đang vẽ nó sai. – mattjgalloway

Các vấn đề liên quan