在UIScrollView上添加UITextView/UILabel

问题描述:

我想在UIScrollview上添加UITextView。然后将UIScrollView添加到UIView。添加scrollView到UIView工作,但将UITextView添加到UISCrollView不起作用。任何指针?在UIScrollView上添加UITextView/UILabel

感谢

UIScrollView * contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(300, 400, 250, 250)]; 
contentScrollView.backgroundColor = [UIColor whiteColor]; 
UITextView * mainContent = [[UITextView alloc]initWithFrame:CGRectMake(300, 400, 200, 200)]; 
mainContent.text = @"HELLO"; 
mainContent.textColor = [UIColor blackColor]; 
[contentScrollView addSubview:mainContent]; 
[contentScrollView setUserInteractionEnabled:YES]; 
[contentView addSubview:contentScrollView]; 

您scrol鉴于高度250和宽度也250但你是给的TextView的框架超出这些限制,即300和400,限制在250,250像

UITextView * mainContent = [[UITextView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)]; 
+0

哦,所以拉布勒的位置是相对的UIScrollView?我认为这是相对于主屏幕。安斯是有效的。谢谢:) – CodeGeek123

+0

没有。它应该是相对于每个事物的父/超视图 – Saad

使用此代码:

UIScrollView * contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 320, 270)]; 
contentScrollView.backgroundColor = [UIColor whiteColor]; 
[contentScrollView setUserInteractionEnabled:YES];  

UITextView * mainContent = [[UITextView alloc]initWithFrame:CGRectMake(60, 30, 200, 200)]; 
mainContent.text = @"HELLO"; 
mainContent.textColor = [UIColor blackColor]; 

[contentScrollView addSubview:mainContent];   
[self.view addSubview:contentScrollView]; 

使用这种技术

UIScrollView * contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 320, 270)]; 

,并将其添加到您的UIScrollView,如:

UITextView * mainContent = [[UITextView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)]; 
contentScrollView.backgroundColor = [UIColor whiteColor]; 
[contentScrollView setUserInteractionEnabled:YES];  
[contentScrollView addSubview:mainContent];