UITextView scrollEnabled = YES在iOS8中设置scrollEnabled = NO后不工作

我创build一个检查UITextView scrollEnabled的演示。 它只包含1个UITextView和2个button启用和禁用滚动

  • 我testing模拟器和设备的iOS 8,如果我点击禁用滚动button, UITextView将禁用正确的滚动,但之后,我点击启用滚动, UITextView 将不会启用滚动

  • 不过,我在设备iOS9上testing,启用和禁用滚动效果很好。

     #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 

任何想法解决这个问题? 如果有人不明白我的解释,请检查我的演示项目
任何帮助或build议将不胜感激

问题是,在iOS 8中,scrollEnabled更改时,contentSize不能正确调整。 你的enableScrollButtonPressed方法的一个小调整将成功解决这个问题。

 -(IBAction)enableScrollButtonPressed:(id)sender { self.myTextView.scrollEnabled = YES; self.myTextView.contentSize = [self.myTextView sizeThatFits:self.myTextView.frame.size]; } 

是的,你是正确的禁用和启用textview的滚动不工作在iOS8它可能是一个错误或其他任何东西,让它。 我们可以通过更改textview的ContentSize来禁用或启用文本视图的滚动。

 #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 

我已经testing了上面的代码,它工作正常,我使用Xcode 7.2.1iOS 8.3 。 试试看,让我知道结果

在这里输入图像说明 search几个小时后,我find了一种方法

 -(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; }); } 

条件是你需要先滚动然后你会完美的工作

意思是当禁用滚动button被点击时,textview应该在某个位置滚动, 一定不要在默认位置

您需要在视图中启用或禁用滚动视图后布局视图。

使用viewWillLayoutSubviews / layoutIfNeeded方法。

我试过我的代码,它正常工作

 IBOutlet UITextView *tv; //---------Enable button action. -(IBAction)enable:(id)sender { tv.scrollEnabled=YES; } //---------Disable button action. -(IBAction)disable:(id)sender { tv.scrollEnabled=NO; } 

在设备上再次尝试您的代码。

改变你的财产如下。

在你的代码中..

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

改变属性如下:

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

那么它会正常工作。 我检查了它。

 Please follow below steps simply self.myTextView.scrollEnabled = NO; self.myTextView.userInteractionEnabled = YES; self.myTextView.scrollEnabled = YES; 

试试这个

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