uiwebview与页眉和页脚

我试图添加页眉和页脚(都作为UIViews ),但由于某种原因,我的页脚坚持到底部,我使用KVO方法来观看我的内容大小。

我在这里提出的方法,我认为这个问题是:

 - (void)updateLayout { // Update the frame of the header view so that it scrolls with the webview content CGRect newHeaderFrame = self.headerView.frame; CGRect newFooterFrame = self.footerView.frame; newHeaderFrame.origin.y = -CGRectGetMinY([self.webView convertRect:self.innerHeaderView.frame toView:self.webView.scrollView]); newFooterFrame.origin.y = self.webView.scrollView.contentSize.height; [self.headerView setFrame:newHeaderFrame]; [self.footerView setFrame:newFooterFrame]; if ([self didTapToFullScreen]) { // The delegate was already called in this case, in the tap gesture callback method return; } BOOL fullScreen = (newHeaderFrame.origin.y < 0); if (([self isZooming] && [self didSwitchToFullScreen]) || (fullScreen == [self isFullScreen])) { return; } [self setSwitchToFullScreen:fullScreen]; [self setFullScreen:fullScreen]; // Call the delegate for the full screen [self.fullScreenDelegate emailView:self showFullScreen:fullScreen]; } 

这是完整的项目

我如何将页脚放置在UIWebView的底部?

您可以在UIWebView滚动视图上设置视图,并摆脱偏移量。

 @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 

另一个select是将顶部填充添加到HTML(“margin-top:100px”),然后标题视图不会出现负偏移。