uiwebview with header and footer

夙愿已清 提交于 2019-12-02 13:43:00

You can set the views on UIWebView scroll view, and fiddle with the offsets.

@interface ViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -100, [UIScreen mainScreen].bounds.size.height, 100)];
    self.headerView.backgroundColor = [UIColor blueColor];
    self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    self.footerView.backgroundColor = [UIColor yellowColor];


    NSString *urlText = @"http://stackoverflow.com/questions/15921974/how-to-load-url-on-launch-in-a-webview-osx-project";
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
    self.webView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - WebView delegate

-(void) webViewDidFinishLoad:(UIWebView *)webView {
    self.webView.scrollView.contentInset = UIEdgeInsetsMake(100.0,0.0,100.0,0.0);


    if(![self.headerView superview])
    {
        [webView.scrollView addSubview:self.headerView];
        [webView.scrollView bringSubviewToFront:self.headerView];
    }

    NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];

    NSInteger height = [result integerValue];

    self.footerView.frame = CGRectMake(0, height, [UIScreen mainScreen].bounds.size.height, 100);

    if(![self.footerView superview])
    {
        [webView.scrollView addSubview:self.footerView];
        [webView.scrollView bringSubviewToFront:self.footerView];
    }

    [self.webView.scrollView setContentOffset:
     CGPointMake(0, -self.webView.scrollView.contentInset.top) animated:NO];
}


@end

Another option would be to add top padding to the HTML ("margin-top:100px") and then the header view would not be in minus offset.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!