最近使用了UIWebView,发现当如下设置时
myWebView.backgroundColor = [UIColor clearColor];
没有实现预期的透明效果
后来加上
myWebView.opaque = NO;
myWebView.backgroundColor = [UIColor clearColor];
-(void)addWebView
{
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 10, 300, 200)];
webView.opaque = NO;
[self.view addSubview:webView];
webView.backgroundColor = [UIColor cleanColor];
NSString *ring = @"<h3>请客劵是什么?</h3><p><span style=\"font-size: 14px; background-color: rgb(0, 176, 240);\">请客劵是网站推送的拉动用户注册的一种优惠措施!</span><br/></p><h3><span style=\"font-size: 14px; background-color: rgb(0, 176, 240);\"><span style=\"font-size: 14px; background-color: rgb(255, 255, 255);\">请客劵有什么类型?</span><br/></span></h3><p><span style=\"font-size: 14px; background-color: rgb(0, 176, 240);\"><span style=\"font-size: 14px; background-color: rgb(255, 255, 255);\"><em>请客劵分为www.ifood517.com<span style=\"font-size: 14px; background-color: rgb(255, 255, 255); color: rgb(0, 112, 192);\">限非会员<span style=\"font-size: 14px; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);\">和<span style=\"font-size: 14px; background-color: rgb(255, 255, 255); color: rgb(0, 112, 192);\">会员之分<span style=\"font-size: 14px; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);\">!</span></span></span></span></em><br/></span></span></p>";
[webView loadHTMLString:ring baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];
webView.delegate = self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// 禁用用户选择
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
// 禁用长按弹出框
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}
关于禁用长按webview后出现的功能项 最近get了一个新的方法, 用手势替换 感觉也是不错的
//添加手势 替换掉webView长按出现的功能选项
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
longPress.delegate = self;
longPress.minimumPressDuration = 0.4; //个人测试感觉0.4秒是最合适的时间
[webView addGestureRecognizer:longPress];
看看关于UIWebView的API
@property(nonatomic,readonly,retain) UIScrollView *scrollView NS_AVAILABLE_IOS(5_0);
webview
上其实是一个scrollview
[webView subviews] objectAtIndex:0]; // 这样就可以找到它了
然后 想避免出现下拉阴影
- //禁掉 `scrollview`的回弹
[(UIScrollView *)[[webView_ subviews] objectAtIndex:0] setBounces:NO];
- //隐藏
for (UIView *subView in [webView_ subviews]) {
if ([subView isKindOfClass:[UIScrollView class]]) {
for (UIView *shadowView in [subView subviews]) {
if ([shadowView isKindOfClass:[UIImageView class]]) {
shadowView.hidden = YES;
}
}
}
}