Transparent UIWebView

Transparent UIWebView

1
2
3
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
[webView loadHTMLString:@"\"background-color: transparent\">..." baseURL:nil];

touchBegan/touchEnd in custom UIWebView

The main disadvantage in the object UIWebView is lack of methods touchBegan and touchEnd. But this question has been settled by some manipulations with privates possibilities:) Thank you Satoshi Nakagawa :)

PSWebView.h – empty

1
2
3
4
5
#import

@interface PSWebView : UIWebView
@end
</uikit>

If you need add method touchBegan then make step by step
1. add after 6 line

1
- (void)webView:(UIWebView*)sender tappedBeganWithTouch:(UITouch*)touch event:(UIEvent*)event;

2. after 11

1
- (void)fireBeganTappedWithTouch:(UITouch*)touch event:(UIEvent*)event;

3. add after 31

1
2
3
4
5
6
7
8
9
10
11
- (void)__touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
     [self __touchesBegan:touches withEvent:event];
     
     id webView = [[self superview] superview];
     if (touches.count == 1) {
          if ([webView respondsToSelector:@selector(fireBeganTappedWithTouch:event:)]) {
               [webView fireBeganTappedWithTouch:[touches anyObject] event:event];
          }
     }
}

4. after 46

1
2
3
     Method targetMethod2 = class_getInstanceMethod(klass, @selector(touchesBegan:withEvent:));
     Method newMethod2 = class_getInstanceMethod(klass, @selector(__touchesBegan:withEvent:));
     method_exchangeImplementations(targetMethod2, newMethod2);

5. and last addition in source before last “@end”

1
2
3
4
5
6
- (void)fireBeganTappedWithTouch:(UITouch*)touch event:(UIEvent*)event
{
     if ([self.delegate respondsToSelector:@selector(webView:tappedBeganWithTouch:event:)]) {
          [(NSObject*)self.delegate webView:self tappedBeganWithTouch:touch event:event];
     }
}

For touchMoves similar steps. It’s easy!

Continue reading

UIWebView get size of content and position of scroll

Simple example get size of content and position of scroll for UIWebView!

1
2
3
4
5
6
int scrollPosition = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];

int sizePage = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"] intValue];

//Scroll UIWebView to point
[webView stringByEvaluatingJavaScriptFromString: [NSString  stringWithFormat:@"window.scrollBy(0,%d);", 100] ];