0

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 ?

el.severo
  • 2,202
  • 6
  • 31
  • 62
  • If you are using auto layout, you should update constraints not frames. You've pinned the view to the bottom of the web view, but you are setting the frame to be off screen. The frame gets set but the layout engine thinks it should be pinned to the bottom of the web view hence the flickering, the view is rapidly jumping back and forth. Also, `newFooterFrame.origin.y = self.webView.scrollView.contentSize.height;` will set the frame of the footer view to be completely off screen at the bottom of the web view's scroll view. You call that line before the view is on screen. Is this what you intend? – beyowulf Apr 16 '16 at 02:26
  • you could use https://github.com/protectedtrust/UIWebView-with-Header-and-Footer – bikram990 Apr 29 '16 at 09:24
  • @bikram990: Thanks, already tried that one. FYI: I've already tried to make this by forking an [appropriate repo](https://github.com/alexszilagyi/HeaderWebView) – el.severo Apr 29 '16 at 09:28
  • @el.severo I was looking for something more stable than that. Thanks for the help! – bikram990 Apr 29 '16 at 09:56

1 Answers1

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.

MCMatan
  • 8,623
  • 6
  • 46
  • 85