I create a demo for checking UITextView scrollEnabled
.
It only contains 1 UITextView
and 2 button enable and disable scroll
I test on
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];
}
Try this one
func scrollViewDidScroll(_ scrollView: UIScrollView) {
tableView.setContentOffset(CGPoint.zero, animated: false)
}
After 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
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.
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.
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