Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView Background Color Always Black

When I add a UIView to a View Controller programmatically I am not able to change the background color to any other color, it always remains black.

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

When I comment drawrect: out and add self.backgroundColor = [UIColor blueColor]; to the initializer the color changes:

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

Why is this and what do I need to change? Actually I would like the background to be transparent.

like image 708
Pete Avatar asked May 11 '14 13:05

Pete


4 Answers

As you are implementing drawRect: this will override any default drawing of the UIView.

You must set the opaque property of your custom view to NO and clear the context before you render the path:

- (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 opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.

like image 85
iOSX Avatar answered Oct 23 '22 12:10

iOSX


backgroundColor of your view is ignored if you draw view yourself using drawRect. Change your code to

- (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];
}
like image 38
Avt Avatar answered Oct 23 '22 12:10

Avt


Set the background color in initWithFrame: method and do your custom drawing inside drawRect:. That should be able to solve your problem. The drawRect: is implemented in order to do custom drawing not to set view specific properties.

@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
like image 2
Sandeep Avatar answered Oct 23 '22 12:10

Sandeep


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

or

enter image description here

I struggled for many hours . If its a model segue, set over current context as property. !

like image 1
Alvin George Avatar answered Oct 23 '22 13:10

Alvin George