21

I create a demo for checking UITextView scrollEnabled. It only contains 1 UITextView and 2 button enable and disable scroll

  • I test on simulator and device iOS 8, if I click the disable scroll button, the UITextView will disable scroll correctly, but after that I click on enable scroll, the UITextView won't enable scroll

  • However, I test on device iOS9, the enable and disable scroll work well.

    #import "ViewController.h"
    
    @interface ViewController ()
    
        @property (weak, nonatomic) IBOutlet UITextView *myTextView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    - (IBAction)enableScrollButtonPressed:(id)sender{
        self.myTextView.scrollEnabled = YES;
    }
    
    - (IBAction)disableScrollButtonPressed:(id)sender{
        self.myTextView.scrollEnabled = NO;
    }
    
    @end
    

Any idea to fix this problem? If someone don't understand my explain please check my demo project
Any help or suggestion would be great appreciated

9
  • in simulator or device May 11, 2016 at 4:37
  • @PKT in both simulator and device
    – Linh
    May 11, 2016 at 4:41
  • i tried you code text view not scrolling until you change orientation May 11, 2016 at 4:50
  • issue is with the auto layout i fixed height of textview and now able to scroll May 11, 2016 at 4:52
  • @PKT I try fixed the height of the textview using autolayout. but my textview still not able to scroll after I click disable then enable scroll :(
    – Linh
    May 11, 2016 at 5:03

8 Answers 8

18
+50

The problem is that in iOS 8, contentSize is not adjusted correctly when scrollEnabled is changed. A small adjustment to your enableScrollButtonPressed method will successfully work around the problem.

-(IBAction)enableScrollButtonPressed:(id)sender
{
    self.myTextView.scrollEnabled = YES;
    self.myTextView.contentSize = [self.myTextView sizeThatFits:self.myTextView.frame.size];
}
1
  • 1
    Looks like this may be back in iOS 12...at least, this fixed a very similar issue for me
    – plivesey
    Nov 16, 2018 at 7:52
6

Yeah you are right disabling and enabling scroll of textview is not working in iOS8 it may be a bug or anything else, let it be. We can disable or enable scroll of text view by just changing the ContentSize of textview .

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *myTextView;

@end

 @implementation ViewController
-(IBAction)enableScrollButtonPressed:(id)sender{
    CGSize scrollableSize = CGSizeMake(self.myTextView.bounds.size.width+20, self.myTextView.bounds.size.height+20);
    [self.myTextView setContentSize:scrollableSize];
}

-(IBAction)disableScrollButtonPressed:(id)sender{
    [self.myTextView setContentOffset:CGPointMake(0, 0)];
    [self performSelector:@selector(StopScroll) withObject:nil afterDelay:0.1];
}

-(void)StopScroll{
    CGSize scrollableSize = CGSizeMake(self.myTextView.bounds.size.width, self.myTextView.bounds.size.height/2);
    [self.myTextView setContentSize:scrollableSize];
}

@end

I had tested the above code it is working fine for me i'am using xcode 7.2.1 and iOS 8.3. Give it a try and let me know the result

2
  • thank you for your effort. this solution work well. However, can you check the @Brett Donald answer, does it better?
    – Linh
    May 18, 2016 at 2:28
  • Welcome Phan,I tried brett donald's code in your demo project something works strange.Try the below scenario 1.Once your app runs, scroll text view to top then disable scroll and enable it again is it working for you ?. 2. Sometimes tapping enable button twice changes the textview frame.
    – Gokul G
    May 18, 2016 at 6:02
5

enter image description hereAfter searching hours i am found one way

-(IBAction)enableScrollButtonPressed:(id)sender{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    self.myTextView.scrollEnabled = YES;
    [self.myTextView setText:self.myTextView.text];

    self.myTextView.layoutManager.allowsNonContiguousLayout = false;

});

}

-(IBAction)disableScrollButtonPressed:(id)sender{

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    self.myTextView.scrollEnabled = NO;
});

}

condition for that is you need to scroll first then you it will work perfectly

Means when disable scroll button is tapped textview should be scrolled at some position must not to at default position

1
  • thank you so much for your effort. This solution work but it will change the text size of TextView after disable then enable scroll. I think the solution given by @Brett Donald is better. Do you think so?
    – Linh
    May 18, 2016 at 2:23
4
Please follow below steps simply
self.myTextView.scrollEnabled = NO;
self.myTextView.userInteractionEnabled = YES;
self.myTextView.scrollEnabled = YES;
3

you need to layout your views after enabling or disabling scroll view in your view.

use viewWillLayoutSubviews/layoutIfNeeded methods.

1
  • 1
    I have used layoutIfNeeded and viewWillLayoutSubviews but it still not work. maybe the way i'm using is wrong. Can you help me in code or please check my demo project
    – Linh
    May 13, 2016 at 2:26
3

change your property as follows.

in your code..

@property (weak, nonatomic) IBOutlet UITextView *myTextView;

change property attribute as follows:

@property (strong, nonatomic) IBOutlet UITextView *myTextView;

then it'll work fine. i checked it.

3
  • ohh.. let me check again. May 13, 2016 at 6:52
  • it is bad to set IBOUTlet to strong May 13, 2016 at 12:25
  • yeah. i was looking for an alternative extraction May 13, 2016 at 12:35
2

I tried my code and it worked properly

IBOutlet UITextView *tv;

//---------Enable button action.

-(IBAction)enable:(id)sender
{
    tv.scrollEnabled=YES;
}

//---------Disable button action.

-(IBAction)disable:(id)sender
{
    tv.scrollEnabled=NO;
}

try your code once more in device.

4
  • did you check it in simulator/device iOS8?
    – Linh
    May 13, 2016 at 2:24
  • i checked it in both simulator and device May 13, 2016 at 3:53
  • yeah sure. let me check May 13, 2016 at 4:18
  • change your property attribute to strong from weak. then it will work fine. i got it working fine. can i show the solution as another answer? i didn't changed anything in your project except this property attribute May 13, 2016 at 5:13
0

Try this one

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    tableView.setContentOffset(CGPoint.zero, animated: false)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.