UITextView顶部边距

我使用的是一个文本视图,并注意到iOS 7默认保留了一个顶部边距。 请参阅以下图片 在这里输入图像说明

我读了不同的post,其中最常见的解决scheme是使用:

[textViewTest setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)]; 

但是这些插页只是特定设备,文本视图,字体大小等的自定义解决scheme。 因此,没有适用于任何解决scheme的具体插入…甚至最糟糕的是,我将不得不以编程方式定义不同的插入来说明所有iOS设备和方向。

好消息是,我发现,无论何时textview成为第一响应者和键盘显示在屏幕上,即使键盘已经消失,这个上边距消失。 顺便说一下,我调整了UIKeyboardDidShowNotification和UIKeyboardWillHideNotification上的contentInset。

  • 当键盘显示时看到图像:

在这里输入图像说明

  • 当键盘消失时看到图像:

在这里输入图像说明

有没有办法模拟键盘显示和隐藏? 所以内容插入消失,如上所述。

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

提前致谢!

我的代码如下:

 - (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; } 

发生这种情况是因为视图控制器已经automaticallyAdjustsScrollViewInsets将属性设置为AdjustsScrollViewInsets为YES,如果将其设置为NO,则一切都会正常。 看到这个问题和更多的信息接受的答案。