Denis Kutlubaev
Denis Kutlubaev

Reputation: 16124

UIView on UIWebView backgroundColor doesn't change

I have a custom UIView subclass instance headerView, which I put on a scrollView of UIWebView. I set a background color of this view to white, when I init it:

self.backgroundColor = [UIColor whiteColor];

This is how I put it on a scrollView of webView:

    webView.scrollView.contentInset = UIEdgeInsetsMake(headerView.bounds.size.height, 0, 0, 0);

    CGRect frame = headerView.frame;
    frame.origin.y -= headerView.bounds.size.height;
    headerView.frame = frame;
    //webView.scrollView.backgroundColor = [UIColor whiteColor];

    [webView.scrollView addSubview:headerView];

If I uncomment the line, the background of my headerView is white. But if I comment it, the background color is the same as the background color of scrollView in webView. So, changing background color of a view in it's init method seems to not work.

Upvotes: 2

Views: 815

Answers (2)

Denis Kutlubaev
Denis Kutlubaev

Reputation: 16124

I found a mistake that caused this problem. I was initing my view like this:

self = [super initWithFrame:CGRectZero];

And then setting frame:

frame = self.frame;
frame.size.height = _subjectView.bottom;
self.frame = frame;

self.backgroundColor = [UIColor whiteColor];
self.opaque = YES;

When I added this in init method:

self.clipsToBounds = YES;

The subviews didn't even show. So my superview's frame width was 0, but subviews still were showing outside it, since their frames were normal and clipsToBounds is NO by default. It was proved by adding this line

frame.size.width = SCREEN_WIDTH;

It started to work.

Upvotes: 2

Flexicoder
Flexicoder

Reputation: 8501

In your subclass have you ensured that its opaque?

    self.view.opaque = YES;

Upvotes: 1

Related Questions