我目前使用以下代码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
滚动回到原来的位置。我已经尝试了几种方法来实现这一点,但我认为我需要一些新鲜的眼睛来看看我做错了什么。
非常感谢任何帮助。
答案 0 :(得分:0)
首先,您不想设置UITextView的contentSize,因为它是由文本长度自动决定的。
尝试使textView的高度更小。例如:
UITextView *textContained = [[UITextView alloc]
initWithFrame:CGRectMake(0, 0, container.view.bounds.size.width, 50)];
并提供更多文字,例如:
textContained.text = @"LOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\n\nLOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\n";