Search code examples
iosobjective-cuiview

Trying to add a subview to a view, but the subview does not show up


I have a circular view which is used as a resize for my object but its color is kinda dominating the object so I decided to add a subview to this view and then color the inner view so that the area of the view remains same while decreasing the color dominance a bit.

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
            UIView *innerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50,  50)];
            [view addSubview:innerView];
            view.layer.cornerRadius = CGRectGetWidth(view.frame) * 0.5;
            view.backgroundColor = [UIColor clearColor];
            innerView.alpha = 0;
            //view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
            innerView.backgroundColor = [UIColor colorWithHexString:@"#866BFF"];
            innerView.layer.cornerRadius = CGRectGetWidth(view.frame) * 0.5;
            innerView.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.8].CGColor;
            innerView.layer.borderWidth = 2;
            innerView.center = view.center;
            [view bringSubviewToFront:innerView];
            [self setGestureRecognizer:view];

            [arr addObject:view];

The resize works fine so the view is being added but the inner view color is not visible. If I change the main views color to something like red etc it works pretty fine.

I don't know why innerView is not being added.


Solution

  • Your innerview is not visible because you alpha is 0 changed to 1.

    innerView.alpha = 1;

    And it works!!