1

我想要一个UIScrollView带有一组子视图的子视图,其中每个子视图都有UITextView一个不同的文本。对于这个任务,我修改了PageControl苹果“iphone dev center”中的示例,以便将其添加UITextView到用于生成滚动视图的子视图的简单视图中。当我运行应用程序(在模拟器和手机上)时,没有看到文本,但如果我激活“用户交互”并单击它,文本会神奇地出现(以及键盘)。

有没有人有解决方案或在UITextView内部取得任何进展UIScrollView?谢谢。

4

6 回答 6

7

我解决了强制“假”滚动的问题:

textView.contentOffset = CGPointMake(0, 1);
textView.contentOffset = CGPointMake(0, 0);
于 2011-05-05T14:00:37.877 回答
2

我认为问题源于它UITextView是 的子类UIScrollView,因此您基本上将滚动视图嵌入其中UIScrollViews。即使文本正确显示,您也会遇到可用性问题,因为如果手指滑动应该滚动外部视图或文本视图,则永远不会清楚。

是的,Safari 有点这样做,但它必须这样做,而且这不是使用 Safari 最令人愉快的部分。

我认为这是困难表明您正在与系统作对的时候之一。我强烈建议回去重新考虑 UI。

于 2008-10-01T04:06:18.223 回答
1

当 UITextView 从屏幕外滚动到屏幕区域时,您可能会遇到 UITextView 无法正确更新的问题。

检查此页面的“屏幕外 UITextViews 未正确更新”部分:UIScrollView 中的多个虚拟页面

我使用的解决方案是在滚动视图开始出现在屏幕上时强制重绘它们。这是一个完全令人讨厌的问题,但确实解决了问题。

于 2009-04-15T11:46:17.413 回答
0

问题很可能是下一个UIScrollViews

我认为有三种解决方案:

  • userInteractionEnabled选择各种控件时启用和禁用。
  • 假设文本是静态的,使用 aUILabel而不是 a UITextView(UILabel不是 的子类UIScrollView)
  • 实现您自己的视图并自己在 drawRect 消息中绘制文本,而不是依赖UITextView.
于 2009-04-15T11:16:27.920 回答
0

我对这个问题的解决方案是不同的:它只有在我将 UIScrollView 的“Autoresize Subviews”属性设置为关闭时才有效。

[scrollView setAutoresizesSubviews:NO];
于 2013-11-30T06:49:07.257 回答
-1

通过将文本值分配放入scrollViewDidScroll方法,它对我有用。

示例片段:


样品.h

...
@interface myRootUIViewController : UIViewController <UIScrollViewDelegate>
...

评论:请记住:不要忘记 UIScrollViewDelegate 协议。


样品.m

    - (void)viewDidLoad {
       ... whatever is created before and/or after...

       NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                           Nunc semper lacus quis erat. Cras sapien magna, porta non, 
                           suscipit nec, egestas in, arcu. Maecenas sit amet est. 
                           Quisque felis risus, tempor eu, dictum ac, volutpat id, 
                           libero. Ut gravida, purus vitae interdum elementum, tortor 
                           justo porttitor nisi, id rhoncus massa.";

       // calculate the required frame height according to defined font size and
       // given text
       CGRect frame = CGRectMake(0.0, 500.0, self.view.bounds.size.width, 1000.0); 
       CGSize calcSize = [text sizeWithFont:[UIFont systemFontOfSize:13.0]
              constrainedToSize:frame.size lineBreakMode: UILineBreakModeWordWrap];
              // for whatever reasons, contraintedToSize seem only be able to
              // calculate an appropriate height if the input frame height is larger
              // than required. Means: if your text requires height=250 and input
              // frame height=100, then this method won't give you the expected
              // result.

       frame.size = calcSize;
       frame.size.height += 0; // calcSize might be not pixel-precise, 
                               // so add here additional padding pixels
       UITextView * tmpTextView = [[UITextView alloc]initWithFrame:frame];

       // do whatever adjustments
       tmpTextView.backgroundColor = [UIColor blueColor]; // show area explicitly (dev 
                                                          // purpose)
       self.myTextView = tmpTextView;
       self.myTextView.editable = NO;
       self.myTextView.scrollEnabled = NO;
       self.myTextView.multipleTouchEnabled = NO;
       self.myTextView.userInteractionEnabled = NO; // pass on events to parentview
       self.myTextView.font = [UIFont systemFontOfSize:13.0];
       [tmpTextView release];
       [self.scrollView addSubview:self.myTextView];
}

...

- (void)scrollViewDidScroll:(UIScrollView *)sender {
  // for simplicity text is repeated again, of course it can be a member var/etc...
  NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                           Nunc semper lacus quis erat. Cras sapien magna, porta non, 
                           suscipit nec, egestas in, arcu. Maecenas sit amet est. 
                           Quisque felis risus, tempor eu, dictum ac, volutpat id, 
                           libero. Ut gravida, purus vitae interdum elementum, tortor 
                           justo porttitor nisi, id rhoncus massa.";
  self.myTextView.text = text; // assign value within this method and it is
                               // painted as expected.
    }

评论:

我显然已经用示例命名和值调整了源代码片段。希望没有错别字。但是,代码还包含文本所需帧高度的计算,以防文本的值可以更改,因此实际上需要不同的帧大小。

将实际的文本值分配到 scrollViewDidScroll 方法对我有用,在滚动等期间没有任何闪烁(到目前为止仅在 iPhone 模拟器中测试过)。

希望有帮助。当然,我愿意接受任何建设性的反馈、改进建议或什至其他解决此问题的方法。

于 2009-04-15T09:52:05.283 回答