uiwebview with header and footer

前端 未结 1 1022
春和景丽
春和景丽 2021-01-28 04:57

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 watchi

相关标签:
1条回答
  • 2021-01-28 05:20

    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.

    0 讨论(0)
提交回复
热议问题