Search code examples
uiviewuiscrollviewsubviewuiscrollviewdelegatelayoutsubviews

UIScrollview overlapping mainview content


Currently, I have this feature in my app where I implemented a UIScrollview that is pretty thin in height but long in width...

Hourly Forecast

As you can see, the UIScrollView is OVERLAPPING the backgroundview... Not that white background is a UIView to which I added the UIScrollView as a SUBVIEW.

Question is, how is the UIScrollView overlapping the black background content when it's just added to the subview?

Here is my init method for the UIScrollView..

self = [super init];
if (self) {
    self.frame = CGRectMake(0, 0, 280, 70);
    self.contentSize = CGSizeMake(480, 70);
    self.showsHorizontalScrollIndicator = NO;
    self.bounces = NO;
}

And here is how I added the UIScrollView to the UIView (whitebackground view) whose name is ForecastView:

_hourlyForecast = [[hourlyForecastScrollView alloc] init:_city state:_state icons:_icons times:_times temps:_temps];
_hourlyForecast.backgroundColor = [UIColor blueColor];
_hourlyForecast.frame = CGRectMake(20, 20, self.ForecastView.bounds.size.width, 70);
[_ForecastView addSubview:_hourlyForecast];

Solution

  • By default, a view will not clip it's subviews. To enable clipping, set the UIView clipsToBounds property, or set it in Interface Builder

    enter image description here

    (credit to Mugunth for the image)