【问题标题】:unwanted scroll space in UITextViewUITextView 中不需要的滚动空间
【发布时间】:2014-11-25 07:09:59
【问题描述】:

我有一个 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;
}

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

请帮忙!

【问题讨论】:

标签: ios uitextview uikeyboard


【解决方案1】:

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

我以前在 viewDidLoad 中设置 textView 的文本是这样的:

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

我关闭了滚动,以便文本视图在设置文本时不会滚动到底部。这就产生了额外滚动的问题。 (我不知道为什么)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多