CodeGeek123
CodeGeek123

Reputation: 4501

Adding UITextView/UILabel on UIScrollView

I am trying to add a UITextView on UIScrollview. and then add the UIScrollView onto a UIView. The adding scrollView to UIView works however adding UITextView to a UISCrollView Does not work. Any pointers?

Thanks

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];

Upvotes: 0

Views: 3775

Answers (3)

user3953272
user3953272

Reputation:

Use this technique

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

and add it into your UIScrollView like:

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

Upvotes: 1

Jay Patel
Jay Patel

Reputation: 144

Use this Code:

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];

Upvotes: 2

Saad
Saad

Reputation: 8947

your height of scrol view is 250 and width also 250. but u are giving the frame of textview beyond these limits i.e. 300, and 400. limit them to 250, 250 like

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

Upvotes: 4

Related Questions