uiwebview with header and footer

℡╲_俬逩灬. 提交于 2019-12-20 06:03:30

问题


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];
}

Here's the full project

How I can place the footer to the bottom of the UIWebView ?


回答1:


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.



来源:https://stackoverflow.com/questions/36656328/uiwebview-with-header-and-footer

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