Friday, October 14, 2011

Adding a fixed image background to UIWebView

A short post for iOS people.

UIWebView doesn't (as of iOS 5.0) honor background-attachment: fixed; in CSS, so if you need a fixed background image on a UIWebView, you're probably searching Google trying to find the answer.

Hopefully, this will help.

UIWebView *someWebView;
someWebView.opaque = NO;
someWebView.backgroundColor = [UIColor clearColor];

UIImageView *imageView;
imageView.image = [UIImage imageNamed:@"something"];
imageView.autoresizingMask = (UIViewAutoresizingFlexibleHeightUIViewAutoresizingFlexibleWidth);
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.frame = someWebView.bounds;
[someWebView insertSubview:imageView atIndex:0];