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

Mahendra
  • 8,448
  • 3
  • 33
  • 56
Linh
  • 57,942
  • 23
  • 262
  • 279

8 Answers8

18

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];
}
Brett Donald
  • 6,745
  • 4
  • 23
  • 51
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

Gokul G
  • 2,046
  • 15
  • 22
  • thank you for your effort. this solution work well. However, can you check the @Brett Donald answer, does it better? – Linh May 18 '16 at 02: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 '16 at 06: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

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • 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 '16 at 02:23
4
Please follow below steps simply
self.myTextView.scrollEnabled = NO;
self.myTextView.userInteractionEnabled = YES;
self.myTextView.scrollEnabled = YES;
Kiran K
  • 919
  • 10
  • 17
3

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

use viewWillLayoutSubviews/layoutIfNeeded methods.

Harish Pathak
  • 1,567
  • 1
  • 18
  • 32
  • 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 '16 at 02: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.

Soorej Babu
  • 350
  • 2
  • 19
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.

Soorej Babu
  • 350
  • 2
  • 19
0

Try this one

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    tableView.setContentOffset(CGPoint.zero, animated: false)
}
Artyom
  • 1,099
  • 15
  • 18