在iOS8中设置scrollEnabled = NO后,UITextView scrollEnabled = YES无法正常工作

Pha*_*inh 21 objective-c uitextview ios

我创建了一个用于检查的演示UITextView scrollEnabled.它只包含1 UITextView和2按钮启用和禁用滚动

  • 我在模拟器和设备iOS 8上测试,如果我单击禁用滚动按钮,UITextView将禁用正确滚动,但之后我点击启用滚动,UITextView 将不会启用滚动

  • 但是,我在设备iOS9上测试,启用和禁用滚动工作正常.

    #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
    
    Run Code Online (Sandbox Code Playgroud)

有什么想法解决这个问题吗?如果有人不理解我的解释请检查 我的演示项目
任何帮助或建议将非常感谢

Bre*_*ald 17

问题是在iOS 8中,更改scrollEnabled时未正确调整contentSize.对enableScrollButtonPressed方法进行小幅调整可以成功解决问题.

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


Gok*_*kul 6

是的,你是正确的禁用和启用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
Run Code Online (Sandbox Code Playgroud)

我已经测试了上面的代码,它使用xcode 7.2.1iOS 8.3正常工作.试一试,让我知道结果


Pra*_*iya 5

在此输入图像描述搜索了几个小时后,我找到了一种方法

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

}
Run Code Online (Sandbox Code Playgroud)

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

意味着当禁用滚动按钮时,轻触textview应该在某个位置滚动, 不得在默认位置