How to Change Background of UIWebView Scrolling? - iphone

in the iPhone SDK Interface Builder, any UIWebView has a default gray background that is shown when the view has been scrolled too far. Is it possible to change this background color so it doesn't look so obvious? The gray conflicts with my App :( Any help would be greatly appreciated.

webView.backgroundColor = yourcolor;
You can even do it in IB.

id scrollView = [[[self yourWebView] subviews] objectAtIndex:0];
if([scrollView respondsToSelector:#selector(setBackgroundColor:)] )
{
[scrollView performSelector:#selector(setBackgroundColor:)
withObject:[UIColor groupTableViewBackgroundColor]];
}

Related

Subclass UITableView to change font colour of the section index?

I have a dark background with a UITableView on top of it. By default the section index is semi-transparent with a dark text colour. I'd like to change the text colour for the section index to the same colour as I have made the UITableViewCell title label. I have read around a bit and it seems you have to subclass the UITableView? How do I do this?
Since iOS 6 you have the possibility to do it like:
searchTable.sectionIndexColor = [UIColor blackColor];
To solve this I used the following in viewDidAppear:
for (UIView *subView in [self.view subviews])
{
if ([[[subView class] description] isEqualToString:#"UITableViewIndex"])
{
[subView performSelector:#selector(setIndexColor:) withObject:[UIColor whiteColor]];
}
}
Since it's not documented, it has to be through a selector.
As of iOS 6.0 there are two methods that allow you to change the color of the section indexes and the background shown when you drag the scrubber.
if ([tableView respondsToSelector:#selector(setSectionIndexColor:)]) {
tableView.sectionIndexColor = ... // some color
tableView.sectionIndexTrackingBackgroundColor = ... // some other color
}
Of course this will only execute if the device has 6.0+. With any older iOS, nothing will change.

iphone:searchbar remove background when interface changes

I have used this code to remove background behind searchbar but when I rotate the view to Landscape or launch the application in landscape view a black container is displayed around searchbar
for (UIView *searchSubview in mySearchBar.subviews) {
NSLog(#"Subview: %#", searchSubview);
if ([searchSubview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
//[searchSubview removeFromSuperview];
[searchSubview setAlpha:0.0];
//[searchSubview setBackgroundColor:[UIColor clearColor]];
//break;
}
}
Please enlighten me on this. I tried to implement this code in willRotate, shouldRotate and didRotate methods but problem persist. Please enlighten me on this.
Regards
Ankit
got it ....
open the interface builder
under searchbar attributes in view, Mode is set as 'Redraw' set it to scaleToFill

background color in above (bounce part) of UIWebView

I think this shouldn't be a big problem, but I can't find the solution on my own. As always :p I have an UIWebView that has background color set to clearColor but when I try to scroll down too much I get the "bouncing area" above loaded HTML in dark gray color. I would like to change this to transparent/white. Is there any way of changing this?
Digression: I read that classes inheriting UIScrollView can have property bounce = NO and then they won't show the bouncing area at all. Even if UIWebView was inheriting that class I wouldn't like to stop it from bouncing, just "bounce it in white" so to speak...
Thanks a lot,
Luka
Take a look at the following answer
Remove gradient background from UIWebView?
Set the webview's background colour and set opaque to NO:
[self.webView setBackgroundColor:[UIColor whiteColor]];
[self.webView setOpaque:NO];
Or try setting clear color as background:
[self.webView setBackgroundColor:[UIColor clearColor]];
[self.webView setOpaque:NO];
#Ladislav: thanks for pointing me to the right subject :)
Now, the following post Remove gradient background from UIWebView? is exactly what I needed, so I am sorry I opened the new thread. I searched the forum but since there is no name for this gradient, bounce background or background I couldn't find it earlier.
The summary would be:
1.Setting the backgroundColor of web view to clearColor !in code! cause in interface builder is not producing the wanted result.
myWebView.backgroundColor = [UIColor clearColor];
this line will only make the "overscroll" background look lighter, but to remove it totally (make it transparent/white) we need to hide all imageViews found in subViews of our myWebView:
for (UIView* subView in [self.myWebView subviews])
{
if ([subView isKindOfClass:[UIScrollView class]]) {
for (UIView* shadowView in [subView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
}
Thanks everyone, I wish you a great weekend

How to reset UIWebView's zoom? I'm already using scalesPagesToFit = YES;

I've been looking for the past week for the answer to this question.
I have a UIWebView, inside of a UIScrollView. Everything works great, but I want the content of the UIWebView to reset its zoom, when the orientation changes.
In the HTML inside the UIWebView, I set the width of the viewport (w/ a meta tag) to "device-width" and then on the Obj-C side, I set the scalesPagesToFit = YES;
I've tried resetting the zoom with javascript; by replacing the meta tags in runtime; reloading; accessing the UIScrollView inside of the UIWebView; etc...
but with no success.
Any of you gods know a workaround?
The only one I can think off is to recreate the UIWebViews every time we change the orientation, but that makes them flash to white whilst rendering content, which looks terrible :(
Any thoughts?
Many thanks,
Andre
I'm just guessing here and haven't tried, but AFAIK a UIWebView has a UIScrollView child. So one should be able to do:
for (UIScrollView *scroll in [myWebView subviews]) {
// Make sure it really is a scroll view and reset the zoom scale.
if ([scroll respondsToSelector:#selector(setZoomScale:)])
[scroll setZoomScale:1.0];
}
On iOS 5+ you have access to scrollView.
Just do:
[webView.scrollView setZoomScale:1.0];
If you want to do it programmatically this is the only way I could find to accomplish it: (specify your own sizes if you wish, i was attempting to zoom out after typing into a form field)
UIScrollView *sv = [[webViewView subviews] objectAtIndex:0];
[sv zoomToRect:CGRectMake(0, 0, sv.contentSize.width, sv.contentSize.height) animated:YES];
Update:
Downscaling wasn't working properly when using
[[[webView subviews] lastObject] setZoomScale:0.25];
The quality of the images being downscaled on the page was awful. Doing:
[[[webView subviews] lastObject] setZoomScale:0.25 animated:YES];
Fixed it. So that last line is the one you could use.
webView was subclassed of a UIWebView which lies on some IB file. I didn't use the Viewport at all. I find that one should pick by either doing this from the Cocoa Touch side or use JS.
I used:
webView.scalesPageToFit = YES;
I wonder if there's a way of resetting the scalesPageToFit.
Adapting from Captnwalker1's answer, I came up with this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(toInterfaceOrientation ==UIInterfaceOrientationPortrait||toInterfaceOrientation==UIInterfaceOrientationMaskPortraitUpsideDown)
{
currentScrollView = [[webView subviews] objectAtIndex:0];
[currentScrollView zoomToRect:CGRectMake(0, 0, currentScrollView.contentSize.width, currentScrollView.contentSize.height) animated:NO];
}
else
{
currentScrollView = [[webView subviews] objectAtIndex:0];
[currentScrollView zoomToRect:CGRectMake(0, 0, currentScrollView.contentSize.width, currentScrollView.contentSize.height) animated:NO];
}
}
So load your webview image, and the image will reset it's size when rotated.

iphone uiwebview initial white view

My app's main view has a uiwebview. It is white for a split second before it has time to render the HTML the app generates.
Is there a way to make the uiwebview black, or another color, before it renders? The flash of white isn't part of my plan for a smooth visual transition.
Objective-C or MonoTouch answers are fine, I am bi-lingual.
Another thing to try is to make the view transparent:
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
This causes additional compositing work, however, so you can reset these values to something more friendly for that after your web view loads, in webViewDidFinishLoad:.
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
webView.backgroundColor = [UIColor blackColor];
webView.opaque = YES;
}
One thing you can try is put a UIView with the color you want and position it above the UIWebView with the same width and height. Then in the webViewDidFinishLoad method, set the UIView to hidden.
I can't comment yet hence the answer.
#Steve, actually it's possible to do with storyboards.
Place a UIView under the UIWebView and:
// Can be set in the storyboard
bottomView.backgroundColor = [UIColor yellowColor];
webview.alpha = 0;
Keep in mind that a lot of page fetch stuff after being loaded via JS so you still can be left with a white page couple seconds after
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
webView.alpha = 1;
}
To add to Steve Madsen's answer (since I can't comment yet). If you instanced the UIWebView in Interface Builder, you wont be able to set alpha to 0.0, but what you can do is bring up the color picker to set a background color (white, black, gray, it doesnt matter), then set the opacity of that color to zero percent, and that works.