带有页眉和页脚的uiwebview [英] uiwebview with header and footer

查看:94
本文介绍了带有页眉和页脚的uiwebview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加页眉和页脚(两者都为 UIViews )但由于某种原因我的页脚粘到底部,我正在使用 KVO 观看我的内容大小的方法。

I am trying to add header and footer (both of them as UIViews) but for some reason my footer sticks to the bottom, I'm using the KVO method for watching my content size.

我在这里介绍我认为问题所在的方法:

I'm presenting here the method where I think the problem is:

- (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滚动视图上设置视图,并拨弄抵消。

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

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

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.

这篇关于带有页眉和页脚的uiwebview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆