- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, ScreenHeight-20)];
// 自动队页面进行缩放以适应屏幕
webView.scalesPageToFit = YES;
webView.userInteractionEnabled = YES;
webView.opaque = YES;
[self.view addSubview:webView];
NSURL * url = [NSURL URLWithString:@"http://www.youku.com"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
// NSString * myHT = @"优酷";
// [webView loadHTMLString:myHT baseURL:url];
/*
[webView goBack]; // 返回
[webView goForward]; // 前往
[webView reload];
[webView stopLoading];
*/
webView.delegate = self;
//移除滚动后的外边阴影
UIScrollView *scrollView = webView.scrollView;
for (int i = 0; i < scrollView.subviews.count ; i++) {
UIView *view = [scrollView.subviews objectAtIndex:i];
if ([view isKindOfClass:[UIImageView class]]) {
view.hidden = YES ;
}
}
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
/**
* typedef NS_ENUM(NSInteger, UIWebViewNavigationType) {
* UIWebViewNavigationTypeLinkClicked,
* UIWebViewNavigationTypeFormSubmitted,
* UIWebViewNavigationTypeBackForward,
* UIWebViewNavigationTypeReload,
* UIWebViewNavigationTypeFormResubmitted,
* UIWebViewNavigationTypeOther
};
*/
NSLOG_FUNCTION;
return YES;
}
// 开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView{
NSLOG_FUNCTION;
}
// 完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLOG_FUNCTION;
}
// 加载失败,弹出错误提示
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription]
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alterview show];
[alterview release];
NSLOG_FUNCTION;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", IPHONE_WIDTH];
[webView stringByEvaluatingJavaScriptFromString:meta];
}
webView.scalesPageToFit = NO;
NSString *js = @"var imgs = document.getElementsByTagName(\"img\");"
"for(var i=0;i<imgs.length;i++){"
" var img = imgs[i];"
" img.onclick=function(){window.location.href=('http://src.'+this.src);}"
"}";
[webView stringByEvaluatingJavaScriptFromString:js];
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *url = request.URL.absoluteString;
if ([url hasPrefix:@"http://src."])
{
url = [url stringByReplacingOccurrencesOfString:@"http://src." withString:@""];
// Do something..
return NO;
}
return YES;
}
CGFloat headerHeight = 36.0f; // 注意:y坐标必须是负数,IPHONE_WIDTH是屏幕宽度 UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -headerHeight, IPHONE_WIDTH, headerHeight)]; [_webView.scrollView addSubview:_headerView]; // 修改webView的scrollView的contentInset,让顶部留出一点空间 UIEdgeInsets edgeInset = _webView.scrollView.contentInset; _webView.scrollView.contentInset = UIEdgeInsetsMake(headerView.frameHeight, edgeInset.left, edgeInset.bottom, edgeInset.right);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有