2014-11-25 132 views
0

我有一個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

回答

2

解決了它。這個問題非常罕見,但如果有人被困住了,這裏就是解決方案。

我用於設置在viewDidLoad中TextView的這樣的文本:

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

我關掉滾動,從而文本視圖不滾動到上設置文本的底部。這就造成了額外滾動的問題。 (我不知道爲什麼)

我用這個解決方案來解決這個問題:https://stackoverflow.com/a/3287419