How to add controls beneath a UIWebView

2020-05-21 05:10发布

What's the simplest, fastest, or otherwise (objectively) best way to add a couple of controls (e.g. some UIButtons) beneath a UIWebView?

To be clear, I'd like to display a regular scrolling UIWebView, but when scrolling reaches the bottom I'd like there to be some UIControls after the web content. I'd rather not fake it with HTML/CSS content that looks like a control; ideally they should be real controls that I can hook up to my view controller as usual.

I don't know whether it's easiest to try to do this with one big UIScrollView containing both the UIWebView and the controls (will there be scrolling conflicts between the UIScrollView and the nested UIWebView?), or with a UITableView containing the web view and controls in separate cells (will performance be terrible?), or in some other way that I haven't thought of.

Update: I don't imagine that I want the UIWebView itself to actually scroll; I thought I'd need to resize it to accommodate all of its content, disable its scrolling behaviour entirely, and then allow the user to scroll its parent view (either the UIScrollView or the UITableView) to reveal different parts of the UIWebView.

3条回答
Deceive 欺骗
2楼-- · 2020-05-21 05:39

I don't believe there is any way to do this within the constraints of the public SDK.

查看更多
做个烂人
3楼-- · 2020-05-21 06:02

Neither the UIScrollView nor UITableView solutions you proposed will work.

If you put the UIWebView in a UIScrollView, one or the other will intercept the scrolls and scroll its content pane, but the other will not. I'm not sure which one will, I think it would be the UIScrollView. In any case, there is no provision for the UIScrollView to scroll when you reach the end of scrolling in the UIWebView, or vice versa.

If you use a UITableView, the UIWebView will scroll inside its cell, but the UITableView won't scroll at all, or the UIWebView won't scroll at all, and the UITableView will. If you use the 'grouped' table style, it's possible that you could scroll the UIWebView and the UITableView, but it would not be the cohesive sort of experience you are looking for.

The only technique I can think of that might work is to muck around in the internals of the UIWebView and add your UIView containing buttons and whatnot to the scrolling entity used by the UIWebView. This is beyond my ken, but these header dumps will get you started. Keep in mind that if Apple notices what you're doing, they may reject your app, and additionally, the internals may change at any time, breaking your code. If you go this route, try to code it in such a way that missing functionality will cause a graceful failure.

EDIT:

With some discussion and experimenting, the OP and I have come to the conclusion that it is very possible, and there is a way to do it that doesn't rely on internals at all. That is, use an enclosing scroll view, and resize the web view once it has loaded to the full size of its own content view. This size can be found using [webView sizeThatFits:CGSizeZero].

If you did want to use UIWebView's privates to accomplish this, I have a short code example of adding a view to the UIWebView's document view, since I got it all worked out anyway :)

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after app launch 
    UIView* testView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
    UIWebView* testWebView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
    [testView addSubview:testWebView];
    [testWebView setDelegate:self];
    [testWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];



    [window addSubview:testView];
    [window makeKeyAndVisible];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    UIView *insert = [[[UIView alloc] initWithFrame:CGRectMake(0, [[webView _documentView] frame].size.height, 320, 40)] autorelease];
    [insert setBackgroundColor:[UIColor greenColor]];
    [[webView _documentView] addSubview:insert];
}
查看更多
家丑人穷心不美
4楼-- · 2020-05-21 06:03

This is how i do it.

- (void)webViewDidFinishLoad:(UIWebView *)webView {


for(UIView *view in [webView subviews]){

    if([view isKindOfClass:[UIScrollView class]]){
        NSLog(@"Scrollview found with tag %d", [view tag]);

        UIScrollView *scroll= (UIScrollView *)view;
        [scroll addSubview:self.finishBtn];
        [finishBtn setCenter:CGPointMake(webView.center.x, scroll.contentSize.height-30)];
    }


}

}

查看更多
登录 后发表回答