1.自定義WebNavigationView作為導(dǎo)航條邑商。
@interface WebNavigationView : UIView
@property UIButton *backbtn;
@property UIButton *forwardbtn;
+(WebNavigationView *)getXinView;
@end
#import "WebNavigationView.h"
#import "AppDelegate.h"
@implementation WebNavigationView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
+(WebNavigationView *)getXinView
{
WebNavigationView *navivie = [[WebNavigationView alloc]initWithFrame:CGRectMake(0, 0, 320, 64)];
navivie.backgroundColor = [UIColor blueColor];
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(10, 20, 44, 44);
[back setImage:[UIImage imageNamed:@"ball"] forState:UIControlStateNormal];
//[back addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
navivie.backbtn = back;
[navivie addSubview:navivie.backbtn];
return navivie;
}
2.將導(dǎo)航條添加到webview上
#import "ViewController.h"
#import "WebNavigationView.h"
@interface ViewController ()<UIScrollViewDelegate,UIWebViewDelegate>
{
UIWebView *webView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 80, 320, 480)];
webView.backgroundColor = [UIColor yellowColor];
//對于uiwebView修改背景顏色無效
//對于webView正中間的那一片白色修改背景顏色無效
webView.scrollView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:webView];
NSString * str = @"<h1>基金封閉期與開放日:基金封閉期為6個(gè)月,基金開放日在每個(gè)月的第二周的周五\n</h1>\n\n<h2>產(chǎn)品特點(diǎn) : <span>明星基金經(jīng)理綁定跟投</span> <span>改革主題</span> <span>超額回報(bào)</span></h2>" ;
[webView loadHTMLString:str baseURL:nil];
//scales縮放
//Fit適合
webView.scalesPageToFit = YES;
//添加導(dǎo)航條到webview上
WebNavigationView *navi = [WebNavigationView getXinView];
[webView addSubview:navi];
self.view.backgroundColor = [UIColor greenColor];
webView.delegate = self;
webView.scrollView.delegate =self;
[navi.backbtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"11111111");
}
//加載完成時(shí)會(huì)觸發(fā)的方法
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '400%'"];
}
-(void)backClick
{
[webView goBack];
NSLog(@"3333333");
}