iOS UIWebView 自适应

来源:互联网 发布:怎么看自己淘宝联盟pid 编辑:程序博客网 时间:2024/06/05 06:10

1.当实例化的UIWebview 设置了属性scalesPageToFit = YES;

当双击UIWebview时,webpage会进行缩放操作,取消webPage放大的操作,在HTML中加入下面字段。

<meta name="viewport" content="width=device-width, initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no" />


2.有时背景太大或页面太长,而我们不想让webview进行滑动,解决方案:

for (id subview in self.webview.subviews)

if ([[subview classisSubclassOfClass: [UIScrollView class]])

{

((UIScrollView *)subview).bounces = NO;

((UIScrollView *)subview).scrollEnabled = NO;

}

3.让webview响应点击(非双击)事件,一是启动UIWebview的用户响应,二是实现委托(点击后的事件):

webview.userInteractionEnabled = YES;

webview.delegate = self;


委托事件实现如下:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{  

    NSURL *requestURL = [ [ request URL ] retain ];  

    // Check to see what protocol/scheme the requested URL is.  

    if ( ( [ [ requestURL scheme ] isEqualToString@"http" ]  

  || [ [ requestURL scheme ] isEqualToString@"https" ] )  

        && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {  

        [webview loadRequest:[NSURLRequest requestWithURL:requestURL]];

return YES;// 返回YES会改变当前Webview的内容,如果是NO就不改变

    }  

    [ requestURL release ];  

    return YES;  

4.当UIWebview请求结束时,如果有服务器返回的东西,可以放在返回的URL里面

- (void)webViewDidFinishLoad:(UIWebView *)webView{

NSString *currentURL = self.wb.request.URL.absoluteString;

NSLog(@"web view finish load URL %@",currentURL);

}


5.UIWebView的透明

[newWebView setBackgroundColor:[UIColor clearColor]];

[newWebView setOpaque:NO];

[newWebView loadHTMLString:[NSString stringWithFormat:@"<html><head><style>body{background-color:transparent;}</style></head><body>%@</body></html>",HTMLContentbaseURL:[NSURL URLWithString:@""]];

0 0