在UIWebView中添加UIButton [英] Add UIButton in a UIWebView

查看:80
本文介绍了在UIWebView中添加UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经由我的请求(loadRequest)填充的UIWebView。

I have a UIWebView which is already filled by my request (loadRequest).

我想在其中添加一个UIButton。编写这个简单的代码很容易:

I would like to add a UIButton in it. It's easy to write this simple code :

[self.myWebView loadRequest:request];
[self.myWebView addSubview:myButton];

但是,似乎UIButton不会滚动UIWebView的内容。它像UIWebView顶部的图层一样保持固定。因此,当用户滚动时,UIButton与内容不同步。

However, it seems the UIButton won't scroll with the UIWebView's content. It stays fixed like a layer on top of the UIWebView. So when the user scrolls, the UIButton gets un-synchronized with the content.

任何想法?

推荐答案

你可以注入html标记,例如
< a href ='yourTag01'> < button>我的按钮< /按钮> < / A>使用stringByEvaluatingJavaScriptFromString方法将
放入UIWebview的内容中。

You can inject html markup such as <a href='yourTag01'> <button>My Button </button> </a> into the content of your UIWebview using stringByEvaluatingJavaScriptFromString method.

要捕获click事件并阻止它重新加载webview的内容,请使用此委托:

To capture the click event and prevent it from reloading content of your webview use this delegate:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if(navigationType==UIWebViewNavigationTypeLinkClicked && [[request.URL absoluteString] isEqualToString: @"yourTag01"])
    {
        //your custom action code goes here
        return NO;
    }
    return YES;
}

这篇关于在UIWebView中添加UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆