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

我读过不同的帖子,其中最常见的解决方案是使用:
[textViewTest setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];但是,这些嵌入只是针对特定设备、文本视图、字体大小等的自定义解决方案。因此,没有适用于任何解决方案的特定信息.甚至最糟糕的情况是,我必须以编程的方式定义不同的嵌入,以说明所有的iOS设备和方向。
好消息是,我发现,每当文本视图成为第一个响应器,并且键盘显示在屏幕上时,即使在键盘消失之后,这个上限也会消失。顺便说一下,我正在调整contentInset在UIKeyboardDidShowNotification和UIKeyboardWillHideNotification上的大小。


有一种模拟键盘显示和隐藏的方法吗?这样内容嵌入就消失了,就像上面解释的那样.。
我已经尝试过让textview成为第一个响应者,然后辞职,但是对于这种方法,用户将不得不看到整个键盘显示隐藏动画。
提前感谢!
我的代码如下:
- (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;
}发布于 2014-02-06 07:13:28
之所以会发生这种情况,是因为you控制器已经将属性automaticallyAdjustsScrollViewInsets设置为YES,如果您将其设置为NO,那么一切都会很好。有关更多信息,请参见此question和已接受的答案。
https://stackoverflow.com/questions/21594684
复制相似问题