UIWebView backgroundColor上的UIView不会改变

时间:2013-01-29 19:04:23

标签: ios uiview uiwebview

我有一个自定义的UIView子类实例headerView,我把它放在UIWebView的scrollView上。我在初始化时将此视图的背景颜色设置为白色:

self.backgroundColor = [UIColor whiteColor];

这就是我把它放在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];

如果我取消注释该行,则headerView的背景为白色。但如果我评论它,背景颜色与webView中scrollView的背景颜色相同。因此,在它的init方法中更改视图的背景颜色似乎不起作用。

2 个答案:

答案 0 :(得分:2)

我发现了一个导致这个问题的错误。我正在这样开始我的观点:

self = [super initWithFrame:CGRectZero];

然后设置框架:

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

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

当我在init方法中添加它时:

self.clipsToBounds = YES;

子视图甚至没有显示。所以我的superview的帧宽是0,但是子视图仍然显示在它之外,因为它们的帧是正常的,并且clipsToBounds默认为NO。通过添加这一行证明了这一点

frame.size.width = SCREEN_WIDTH;

它开始起作用了。

答案 1 :(得分:1)

在您的子类中,您是否确保其不透明?

    self.view.opaque = YES;
相关问题