UITextView,用户滚动UITextView,但然后它恢复到原来的位置,有什么问题



我目前有一个UITextView,它包含在使用以下代码的UIViewController中:

UIViewController *container = [[UIViewController alloc] init];
container.view.frame = CGRectMake(0, 0,
[UIScreen mainScreen].bounds.size.width, 1000);
//[UIScreen mainScreen].bounds.size.height
container.view.userInteractionEnabled = YES;
container.view.clipsToBounds = YES;
UIImageView *imgView = [[UIImageView alloc] 
initWithImage:[UIImage imageNamed:@"myImage.png"]];
imgView.frame = container.view.frame;
imgView.userInteractionEnabled = YES;
[container.view addSubview:imgView];
[imgView release];
UITextView *textContained = [[UITextView alloc] 
initWithFrame:CGRectMake(0, 0, container.view.bounds.size.width, 1000)];
//container.view.bounds.size.height
textContained.font = [UIFont fontWithName:@"Calibri" size:14];
textContained.scrollEnabled = YES;
textContained.editable = NO;
textContained.textColor = [UIColor whiteColor];
textContained.backgroundColor = [UIColor clearColor];
textContained.font = [UIFont boldSystemFontOfSize:14];
textContained.userInteractionEnabled = YES;
textContained.alwaysBounceVertical = YES;
textContained.contentSize = CGSizeMake(container.view.frame.size.width,
container.view.frame.size.height);

然后我用一些文本设置我的UItextView文本属性,它扩展超过当前屏幕大小。然后使用以下代码将UITextView添加到容器中。

 switch (indexPath.row) {
    case 0:
        textContained.text = @"LOTS AND LOTS OF TEXT";
        [container.view addSubview:textContained];
        [self.navigationController pushViewController:container animated:YES];
        [textContained release];
        break; 
    default:
        break;
 }
 [container release];
当我测试这段代码时,文本在UITextView中显示得很好,一切看起来都很好。但问题是,当我试图向下滚动,以看到其余的文本。每次向下滚动,UITextView都会滚动回原来的位置。我已经尝试了几种方法来让它工作,但我想我需要一些新鲜的眼睛来看看我做错了什么。

首先,您不需要设置UITextView的contentSize,因为它是由文本的长度自动确定的。

尝试使textView的高度更小。例如:

UITextView *textContained = [[UITextView alloc] 
initWithFrame:CGRectMake(0, 0, container.view.bounds.size.width, 50)];

并添加更多文本,如:

textContained.text = @"LOTS AND LOTS OF TEXTnLOTS AND LOTS OF TEXTnLOTS AND LOTS OF TEXTnnLOTS AND LOTS OF TEXTnLOTS AND LOTS OF TEXTn";

最新更新