How to set background color to the content area in UIScrollView?
Solution 1
The trick is to add subviews to the ScrollView that sit above and below the content. This way, they only appear when the content is dragged up or down.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, -200, scroll.bounds.size.width, 200)];
[topView setBackgroundColor:[UIColor darkGrayColor]];
[scroll addSubview:topView];
[topView release];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, scroll.bounds.size.height, scroll.bounds.size.width, 200)];
[bottomView setBackgroundColor:[UIColor darkGrayColor]];
[scroll addSubview:bottomView];
[bottomView release];
Solution 2
just set backgroundColor.
[scrollview setBackgroundColor:[UIColor whiteColor]];
Comments
-
CKT almost 2 years
I am using an
UIScrollView
in my application, my requirement is I have to add background color to theUIScrollView
but when user drags at the extreme top or at the extreme bottom the area other than the content/out side the content which shows in the scroll view should be shown with different color just like inUIWebView
. In web view if we drag down at the top or drap up at the bottom it will show default background image/gray color and the web view content background color is diff(default white).Can anyone help me in achieving this.thank you.