如何在UIWebView下添加控件 [英] How to add controls beneath a UIWebView

查看:71
本文介绍了如何在UIWebView下添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UIWebView下面添加几个控件(例如一些UIButtons)的最简单,最快或其他(客观)最佳方法是什么?



要清楚,我想显示一个常规的滚动UIWebView,但当滚动到达底部时,我希望在Web内容之后有一些UIControls。我宁愿不使用看起来像控件的HTML / CSS内容来伪造它;理想情况下,它们应该是真正的控件,我可以像往常一样连接到我的视图控制器。



我不知道用一个大的UIScrollView尝试这样做是否最简单既包含UIWebView又包含控件(UIScrollView和嵌套的UIWebView之间会有滚动冲突吗?),还是包含单独单元格中的Web视图和控件的UITableView(表现会很糟糕?),还是以其他方式我没有想到。



更新:我不认为我希望UIWebView本身实际滚动;我认为我需要调整它以适应其所有内容,完全禁用其滚动行为,然后允许用户滚动其父视图(UIScrollView或UITableView)以显示UIWebView的不同部分。

解决方案

您建议的UIScrollView和UITableView解决方案都不起作用。



如果你把UIWebView放在一个UIScrollView中,一个或另一个将截取滚动并滚动它的内容窗格,但另一个不会。我不确定哪一个会,我认为它将是UIScrollView。在任何情况下,当您到达UIWebView中的滚动结束时,UIScrollView都没有滚动,反之亦然。



如果您使用UITableView, UIWebView将在其单元格内滚动,但UITableView根本不会滚动,或者UIWebView根本不会滚动,而UITableView会滚动。如果你使用'分组'表格样式,你可能会滚动UIWebView和UITableView,但它不会是你正在寻找的有凝聚力的经验。



<我能想到的唯一可行的方法是在UIWebView的内部进行处理,并将包含按钮的UIView添加到UIWebView使用的滚动实体中。这超出了我的范围,但这些标头转储将帮助您入门。请记住如果Apple注意到您正在做什么,他们可能会拒绝您的应用,此外,内部可能会随时更改,从而破坏您的代码。如果你走这条路线,试着对它进行编码,使得缺少的功能会导致优雅的失败。



编辑:



通过一些讨论和实验,OP和我得出的结论是,它很有可能,并且有一种方法可以完全不依赖于内部。也就是说,使用封闭滚动视图,并在Web视图加载到其自己的内容视图的完整大小后调整其大小。可以使用[webView sizeThatFits:CGSizeZero]找到此大小。



如果你确实想使用UIWebView的私有来完成这个,我有一个简短的代码示例,在UIWebView的文档视图中添加一个视图,因为我把它全部解决了无论如何:)

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

//覆盖应用程序启动后自定义点
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];
}


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.

解决方案

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

这篇关于如何在UIWebView下添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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