UITextView中不需要的滚动空间

问题描述:

我有一个UITextView覆盖故事板中的整个视图控制器设置。顶部布局指南,底部布局指南,前缘和后缘都有限制。UITextView中不需要的滚动空间

我已经注册键盘的通知,调整内容的插图是这样的:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardAppeared:) 
              name:UIKeyboardDidShowNotification 
              object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardDisappeared:) 
              name:UIKeyboardDidHideNotification 
              object:nil]; 

keyboardAppeared的实现:

- (void)keyboardAppeared:(NSNotification *)notification { 
    NSDictionary *notificationUserInfo = [notification userInfo]; 
    CGRect keyboardRect = [[notificationUserInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    self.textView.contentInsets = UIEdgeInsetsMake(0, 0, keyboardRect.heignt, 0); 
    self.textView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, keyboardRect.height, 0); 
} 

keyboardDisappeared的实现:

- (void)keyboardDisappeared:(NSNotification *) { 
    self.textView.contentInsets = UIEdgeInsetsZero; 
    self.textView.scrollIndicatorInsets = UIEdgeInsetsZero; 
} 

这里的问题是,当键盘出现在tex中有不需要的滚动tView如果textView文本较少。当文字大小超过textView高度时,不需要的滚动不会出现。

请帮忙!

+0

看到这个答案http://stackoverflow.com/questions/25782352/what-is-causing-this-unwanted- content-inset-with-uitextview -in-ios-8-not-there – Deepesh 2014-11-25 07:15:06

+0

链接没有帮助。在文本结尾处我获得了额外的空间。我已经自动将AdjustScrollViewInsets切换到NO。 – Tarun 2014-11-25 07:47:11

解决了它。这个问题非常罕见,但如果有人被困住了,这里就是解决方案。

我用于设置在viewDidLoad中TextView的这样的文本:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.textView setScrollEnabled:YES]; 
    [self.textView setText:#-some text-#]; 
    [self.textView setScrollEnabled:NO]; 
} 

我关掉滚动,从而文本视图不滚动到上设置文本的底部。这就造成了额外滚动的问题。 (我不知道为什么)

我用这个解决方案来解决这个问题:https://stackoverflow.com/a/3287419