2014-05-11 25 views
15

Khi tôi thêm UIView vào Bộ điều khiển chế độ xem theo chương trình, tôi không thể thay đổi màu nền thành bất kỳ màu nào khác, màu này luôn luôn đen.Màu nền UIView luôn màu đen

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    self.backgroundColor = [UIColor blueColor]; 

    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    [path moveToPoint:CGPointMake(100, 33)]; 
    [path addLineToPoint:CGPointMake(200, 33)]; 
    path.lineWidth = 5; 
    [[UIColor redColor] setStroke]; 
    [path stroke]; 
} 

Khi tôi nhận xét drawrect: ra và thêm self.backgroundColor = [UIColor blueColor]; để initializer những thay đổi màu sắc:

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

Tại sao điều này và làm những gì tôi cần phải thay đổi? Thực ra tôi muốn nền trong suốt.

Trả lời

13

backgroundColor của tầm nhìn của bạn bị bỏ qua nếu bạn vẽ xem mình sử dụng drawRect. Thay đổi mã của bạn thành

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    [[UIColor blueColor] setFill]; // changes are here 
    UIRectFill(rect);    // and here 

    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    [path moveToPoint:CGPointMake(100, 33)]; 
    [path addLineToPoint:CGPointMake(200, 33)]; 
    path.lineWidth = 5; 
    [[UIColor redColor] setStroke]; 
    [path stroke]; 
} 
+4

Trả lời hoạt động hoàn hảo khi tôi muốn có màu nền. Nhưng tôi không thể sử dụng [[UIColor clearColor] setFill]; Nó vẫn có màu đen. – Pete

+3

@Pete, tôi thấy kết quả tương tự như bạn làm khi cố gắng thiết lập màu sắc rõ ràng trong drawRect. Để khắc phục điều đó, xóa cuộc gọi UIRectFill đó và thiết lập màu nền để xóa trong phương thức init của khung nhìn (phương thức init nào là phù hợp với cách bạn tạo khung nhìn). – rdelmar

+1

@Pete Vui lòng kiểm tra câu hỏi này http://stackoverflow.com/questions/2125543/setting-a-cgcontext-transparent-background.Có lẽ nó sẽ giúp – Avt

21

Khi bạn đang triển khai drawRect:, điều này sẽ ghi đè lên bất kỳ bản vẽ mặc định nào của UIView.

Bạn phải thiết lập các opaque tài sản của giao diện tùy chỉnh của bạn để NO và rõ ràng bối cảnh trước khi bạn làm cho đường dẫn:

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

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(context, rect); 

    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    [path moveToPoint:CGPointMake(100, 33)]; 
    [path addLineToPoint:CGPointMake(200, 33)]; 
    path.lineWidth = 5; 
    [[UIColor redColor] setStroke]; 
    [path stroke]; 
} 

https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instp/UIView/opaque

An xem đục được kỳ vọng sẽ lấp đầy giới hạn của nó với nội dung hoàn toàn mờ đục — tức là nội dung phải có giá trị alpha là 1.0. Nếu chế độ xem mờ và không lấp đầy các giới hạn của nó hoặc chứa toàn bộ nội dung trong suốt hoặc một phần trong suốt, kết quả là không thể đoán trước. Bạn phải luôn đặt giá trị của thuộc tính này thành NO nếu chế độ xem là minh bạch hoàn toàn hoặc một phần.

+0

Khi tôi sử dụng lệnh này, nền vẫn đen vì một lý do nào đó. Giải pháp không hiệu quả với tôi. Tại sao điều này có thể? – Pete

+1

Tôi đã sửa câu trả lời của mình. Điều quan trọng là phải đặt thuộc tính mờ của chế độ xem là KHÔNG. – iOSX

+0

Hoàn hảo, cảm ơn. Hoạt động tốt ngay bây giờ. – Pete

1

Đặt màu nền trong phương thức initWithFrame: và vẽ tùy chỉnh bên trong drawRect :. Điều đó sẽ có thể giải quyết vấn đề của bạn. DrawRect: được thực hiện để làm bản vẽ tùy chỉnh không đặt thuộc tính cụ thể xem.

@implementation MyView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor yellowColor]; 
    } 
    return self; 
} 
- (void)drawRect:(CGRect)rect{ 
    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    [path moveToPoint:CGPointMake(100, 33)]; 
    [path addLineToPoint:CGPointMake(200, 33)]; 
    path.lineWidth = 5; 
    [[UIColor redColor] setStroke]; 
    [path stroke]; 

} 

@end 
1
backgroundViewController.providesPresentationContextTransitionStyle = YES; 
backgroundController.definesPresentationContext = YES; 
[overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext]; 

hoặc

enter image description here

tôi vật lộn trong nhiều giờ. Nếu một mô hình của nó phân biệt, hãy đặt trên ngữ cảnh hiện tại làm thuộc tính. !

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