首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITextView上缘

UITextView上缘
EN

Stack Overflow用户
提问于 2014-02-06 05:22:37
回答 1查看 3.8K关注 0票数 7

我使用的是文本视图,并注意到默认情况下,iOS 7保留了一个顶部空白。见下面的图像

我读过不同的帖子,其中最常见的解决方案是使用:

代码语言:javascript
复制
[textViewTest setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];

但是,这些嵌入只是针对特定设备、文本视图、字体大小等的自定义解决方案。因此,没有适用于任何解决方案的特定信息.甚至最糟糕的情况是,我必须以编程的方式定义不同的嵌入,以说明所有的iOS设备和方向。

好消息是,我发现,每当文本视图成为第一个响应器,并且键盘显示在屏幕上时,即使在键盘消失之后,这个上限也会消失。顺便说一下,我正在调整contentInset在UIKeyboardDidShowNotification和UIKeyboardWillHideNotification上的大小。

  • 当键盘显示时,请参见图像:

  • 当键盘消失时,请参阅图像:

有一种模拟键盘显示和隐藏的方法吗?这样内容嵌入就消失了,就像上面解释的那样.

我已经尝试过让textview成为第一个响应者,然后辞职,但是对于这种方法,用户将不得不看到整个键盘显示隐藏动画。

提前感谢!

我的代码如下:

代码语言:javascript
复制
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    if(self.topMarginIsAlreadyResized == NO) {
        [self.myTextView becomeFirstResponder]; // Keyboard will show to eliminate top margin when view appears
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)handleKeyboardDidShow:(NSNotification *)notification {
    if(self.topMarginIsAlreadyResized == NO) {
        self.topMarginIsAlreadyResized = YES; // Once that keyboard has shown when view appears, we should hide it manually
        [self.myTextView resignFirstResponder];
    }
    NSValue *keyboardRectAsObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = CGRectZero;
    [keyboardRectAsObject getValue:&keyboardRect];
    self.myTextView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, keyboardRect.size.height, 0.0f);
}

- (void)handleKeyboardWillHide:(NSNotification *)notification {
    self.myTextView.contentInset = UIEdgeInsetsZero;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-06 07:13:28

之所以会发生这种情况,是因为you控制器已经将属性automaticallyAdjustsScrollViewInsets设置为YES,如果您将其设置为NO,那么一切都会很好。有关更多信息,请参见此question和已接受的答案。

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21594684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档