带有页眉和页脚的 uiwebview

uiwebview with header and footer

我正在尝试添加页眉和页脚(两者都是 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];
}

Here's the full project

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

您可以在 UIWebView 滚动视图上设置视图,并且 fiddle 具有偏移量。

@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://whosebug.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

另一种选择是将顶部填充添加到 HTML ("margin-top:100px"),然后 header 视图将不会处于负偏移。