【问题标题】:inserting various views into UIScrollView将各种视图插入 UIScrollView
【发布时间】:2009-07-23 20:50:33
【问题描述】:

我们正在尝试为类似 RSS 的应用程序实现文章详细信息视图。窗口有一个 UIScrollView,其中包含一个 UITextView(它是标题)、一个 UIButton(它打开一个画廊)和一个 UIWebView(带有正文)。这个想法是所有这些元素一起滚动......

问题是,每当我们的身体超过 1000 像素高时,低于该标记的所有内容都会被截断。我们听说这是因为这是视图的最大高度,而且由于 Scroll 是由顶部的 UIScrollView 处理的,因此无法看到更下方的内容。

有人知道解决办法吗?

谢谢!

---- 更新 1 ----

我们已将正文分成 900 像素高的块,但第一个之后的所有 webview 都没有显示内容...

UIScrollView *scroll=[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[scroll setBackgroundColor:[UIColor whiteColor]];
[scroll setCanCancelContentTouches:NO];
scroll.clipsToBounds = NO;  
scroll.indicatorStyle = UIScrollViewIndicatorStyleBlack;

//Título
CGRect tit =CGRectMake(5.0f, 0.0f, 315.f, 50.0f);
UILabel *titulo=[[UILabel alloc] initWithFrame:tit];
titulo.text=[itemNew objectForKey:@"titulo"];   
titulo.numberOfLines = 2;
titulo.font=[UIFont fontWithName:@"Helvetica" size:16.0f];
titulo.textColor=[self getColor:@"00336E"];
[scroll addSubview:titulo];
[titulo release];   

//Entradilla
float altura_entradilla=calcLabel([itemNew objectForKey:@"entradilla"], [UIFont boldSystemFontOfSize:16], 50, 315.0f);
CGRect ent_frame = CGRectMake(5.0f, 50.0f, 315.0f,altura_entradilla );
UILabel *entradilla=[[UILabel alloc] initWithFrame:ent_frame];
entradilla.text=[itemNew objectForKey:@"entradilla"];
entradilla.numberOfLines=4;
entradilla.font=[UIFont fontWithName:@"Helvetica" size:17.0f];
entradilla.textColor=[UIColor blackColor];
[scroll addSubview:entradilla];
[entradilla release];

NSString *cuerpo=[itemNew objectForKey:@"cuerpo"];

[scroll setContentSize:CGSizeMake(320.0f,40000.0f)];
[scroll setScrollEnabled:YES];  

webView = [[UIWebView alloc] initWithFrame:CGRectMake(5.0f, altura_entradilla+50, 315.0f, 900)];    
[webView loadHTMLString:cuerpo baseURL: [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];  
webView.detectsPhoneNumbers=NO;
webView.delegate=self;
[webView setScalesPageToFit:NO];
[scroll addSubview:webView];

webView.backgroundColor=[UIColor blackColor];
webView.userInteractionEnabled=NO;
[webView release];

if (altura_webview>900) {
   UIWebView *webView2 = [[UIWebView alloc] initWithFrame:CGRectMake(5.0f, altura_entradilla+50+900, 315.0f, 900)]; 
   [webView2 loadHTMLString:cuerpo baseURL: [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];  
   webView2.detectsPhoneNumbers=NO;
   webView2.delegate=self;
   [webView2 setScalesPageToFit:NO];
   [scroll addSubview:webView2];
   webView2.backgroundColor=[UIColor yellowColor];
   webView2.userInteractionEnabled=NO;
   [webView2 release];
}

self.view=scroll;

【问题讨论】:

  • 我不知道这是否可行,但是是否可以将 UIText 和 UIButton 插入到 UIWebView 中?
  • WebView 有 webView.userInteractionEnabled=YES;以便所有视图通过 UIScrollView 一起滚动。

标签: iphone cocoa


【解决方案1】:

检查您的 UIScrollview 上的 contentSize

我没有听说过 1000 点的限制(或者在我的应用中的更大视图中看到它)。

【讨论】:

  • developer.apple.com/iphone/library/documentation/UIKit/… UIView 实例的最大高度和宽度为 1024 x 1024。大于此值的视图必须使用 CATiledLayers 创建。
  • 这并不完全正确。尽管文档说最大纹理大小为 1024,但在原始 iPhone 和 3G iPhone 上,CALayers 和 UIViews 的最大大小实际上是 2048 x 2048。
【解决方案2】:
  1. 使用多个 UIWebViews 作为正文

  2. 您是否使用 loadHTMLString 向 webView 添加内容? 试试 loadRequest 方法。使用loadRequest时1024px没有问题

【讨论】:

  • 您好:我们使用 loadHTMLString 是因为 HTML 存储在本地变量中(保存在前一个会话中)。 loadRequest 只能在从 URL 加载时使用,对吗?问题似乎是,当滚动由父 UIScroll 而不是 UIWebView 处理时,它施加了 1024px 的限制。
  • 嗯...在我当前的项目中,我使用 UIScrollViews 的 contentSize.height 超过 40000px。 (许多 webViews、Labels 和 ImageViews)并且滚动没有任何问题。但是如果我尝试创建 UIWebView 最高然后 1024px 我有问题 loadRequest 可用于从本地文件加载数据
  • 我们尝试将 body 分成几个 900px 高的 webViews,但是第二个、第三个等被空白(你可以看到背景颜色,但看不到内容)......我'已编辑问题以显示代码。
  • 安东尼奥,是的。如果您将 webView 或 TextView 放入 ScrollView,但在 scrollView 的不可见部分,则会发生这种情况。当它们到达可见区域时,您必须重绘它们...要重绘,您可以(例如)更改现在可见 webView 的帧大小...
  • 再次嗨:我找到了这个,它真的很有帮助......pinchzoom.com/blog/items/view/1386/…
【解决方案3】:

我遇到了同样的问题,我阅读并尝试了该线程的一些提示,但无论是通过 loadHTMLString 加载内容还是使用 [_webView setNeedsDisplay] 都没有改变我的任何东西。 (我应该尝试加载更多的 UIWebViews,但我很懒:)

我使用滚动视图委托进行了快速而肮脏的修复,它成功了:

@interface ArticleViewController : UIViewController <UIScrollViewDelegate> 

然后在 init 中启动视图

_scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = _scrollView;
_scrollView.delegate = self;

并使用 delgate 的方法在 webView 出现时重绘它

- (void) scrollViewDidEndDragging: (UIScrollView *) scrollView willDecelerate: (BOOL) willDecelerate
{
    CGRect frame = _webView.frame;
    frame.size = CGSizeMake(frame.size.width, frame.size.height + 1);
    _webView.frame = frame;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    CGRect frame = _webView.frame;
    frame.size = CGSizeMake(frame.size.width, frame.size.height + 1);
    _webView.frame = frame;
}

【讨论】:

    【解决方案4】:

    当你说:

    我们尝试将主体分解为几个 900 像素高的 webView,但第二个、第三个等被空白(您可以看到背景颜色,但看不到内容)...

    在我看来,其他人也遇到过这个问题(我再也找不到帖子了),实际上 UIWebviews 似乎只使用了一个 HTML 解释器。这意味着如果一个 UIWebView 正在加载 HTML 内容,其他 UIWebView 将不会加载任何内容。

    要解决这个问题,你需要使用 UIWebView 的代理方法:didFinishLoad,然后开始加载其他 webview。

    请注意,我不确定我在说什么,因为我自己没有尝试过,但这可能是一个解决方案。

    【讨论】:

      【解决方案5】:

      标记为答案的帖子在其最后一条评论中有一个链接。这就是这个问题的解决方案。我遇到了同样的问题,并调用 [[webView _documentView] _webCoreNeedsDisplay];在这 2 位代表中解决了它。干杯。

      【讨论】:

      • 我尝试了相同的解决方案,但 Apple 拒绝了我的应用,并评论说“3.3.1 应用程序只能以 Apple 规定的方式使用已记录的 API,并且不得使用或调用任何私有 API。”您的应用程序中包含的非公共 API 是 _documentView 这个问题还有其他解决方案吗?谢谢水壶
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多