ios-webview上添加导航条

1.自定义WebNavigationView作为导航条。

@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.将导航条添加到webview上

    #import "ViewController.h"
#import "WebNavigationView.h"
@interface ViewController ()
{
        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 = @"

基金封闭期与开放日:基金封闭期为6个月,基金开放日在每个月的第二周的周五\n

\n\n

产品特点 : 明星基金经理绑定跟投 改革主题 超额回报

" ; [webView loadHTMLString:str baseURL:nil]; //scales缩放 //Fit适合 webView.scalesPageToFit = YES; //添加导航条到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"); } //加载完成时会触发的方法 -(void)webViewDidFinishLoad:(UIWebView *)webView { [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '400%'"]; } -(void)backClick { [webView goBack]; NSLog(@"3333333"); }

你可能感兴趣的:(ios-webview上添加导航条)