bapi
bapi

Reputation: 1943

UITextView is not scrolling

I want to display one paragraph in UITextView. I'm able to put the content of paragraph in Text view, but I'm unable to scroll UITextView (Some of the content is not visible as it's not scrolling).

What I'm trying to do is:

CGRect frame = CGRectMake(0, 0, 320, 480);
//allocate view
self.view = [[UIView alloc]initWithFrame:frame];textview = [[UITextView alloc]initWithFrame:CGRectMake(30, 200, 275, 400)];
[textview setFont:[UIFont fontWithName:@"Arial" size:12]];
[textview setScrollEnabled:YES];
[textview setUserInteractionEnabled:NO];
[textview setBackgroundColor:[UIColor clearColor]];
//[textview setText:@"Hi,I'm working fine with this space"];
[self.view addSubview:textview];

In this case, scrolling is enabled. Can any one tell me why it's not scrolling?

Upvotes: 13

Views: 32658

Answers (8)

D. Rothschild
D. Rothschild

Reputation: 719

I have a ViewController that only has a text view with about 5 pages of text.

Here is what I did in Swift 4.0:

  1. Very important: Make sure you have auto layout set on the text view. In my ViewController, I set my constraints to the text view to 8,8,8,8 with constrain to margins checked. The whole thing does not scroll if auto layout is not set.
  2. This is the code I used:

Swift 4.0

textForPrivacyPolicy.showsVerticalScrollIndicator = true
textForPrivacyPolicy.isEditable = false
textForPrivacyPolicy.isScrollEnabled = true
textForPrivacyPolicy.scrollRangeToVisible(NSMakeRange(0, 0))
textForPrivacyPolicy.setContentOffset(CGPoint(x: 0, y: 0), animated: false)

Upvotes: 4

Ashu
Ashu

Reputation: 3523

bapi rout , There is a property for the user interaction with textView. You have set it to No. If you want to perform scroll on TextView, You have to enable this property.

[textview setUserInteractionEnabled:YES];

This will help you.

Upvotes: 0

Rahul
Rahul

Reputation: 51

I had the same issue and I had setscrollable and userinteractionenabled to TRUE. It turned out to be a problem with Constraints. In order to fix this, click on your view controller in ur storyboard (the battery icon on top right), then go to "Editor"->"Resolve Auto Layout Issues"->"Add Missing Constraints In ". Fixed the issue for me!

Upvotes: 0

Nikita P
Nikita P

Reputation: 4246

Set UserInteractionEnabled as YES.

Upvotes: 2

Vishal
Vishal

Reputation: 8256

Add this line : [textview setUserInteractionEnabled:YES]; in place of this:

[textview setUserInteractionEnabled:NO];

Upvotes: 2

Ahsan
Ahsan

Reputation: 825

set [textview setUserInteractionEnabled:TRUE];and I think you dont want to keyboard to pop up then you can hide the keyboard by implementing the uitextview delegate methode -(void)textViewDidBeginEditing:(UITextView *)textView{[textView resignFirstResponder];

Upvotes: 20

P.J
P.J

Reputation: 6587

Paste this code

CGRect frame = CGRectMake(0, 0, 320, 480);
//allocate view
self.view = [[UIView alloc]initWithFrame:frame];textview = [[UITextView alloc]initWithFrame:CGRectMake(30, 200, 275, 400)];
[textview setFont:[UIFont fontWithName:@"Arial" size:12]];
[textview setScrollEnabled:YES];
[textview setUserInteractionEnabled:YES];
[textview setBackgroundColor:[UIColor clearColor]];
//[textview setText:@"Hi,I'm working fine with this space"];
[self.view addSubview:textview];

Upvotes: 2

Paras Joshi
Paras Joshi

Reputation: 20541

set YES for setUserInteractionEnabled property of UITextView

set this line

[textview setUserInteractionEnabled:YES];

instead of

[textview setUserInteractionEnabled:NO];

Upvotes: 7

Related Questions