![](/img/trans.png)
[英]UIWebView within a UIScrollView like the Apple Mail.app
[英]Autoscroll UIScrollView to fit content like in native iOS Mail.app
创建新字母时,本机iOS Mail.app
具有强大的功能。 整个屏幕是一个UIScrollView
,而写字母正文的位置是一个UITextView
,该滚动被禁用。
当您键入此TextView
的高度以及UIScrollView
的高度时,其动态变化,并且UIScrollView
向下滚动,从而在键盘上方留出一些像素来输入新文本。
我知道这个过程已在做textViewDidChange
的方法,但在试图做同样的,不顺心的事在我的代码- UITextField
有时可能会往下走,下UIScrollView
。 这是我尝试执行的操作:
-(void)textViewDidChange:(UITextView *)textView {
CGRect frame = emailTextView.frame;
frame.size.height = emailTextView.contentSize.height;
emailTextView.frame = frame;
mainScrollView.contentSize = CGSizeMake(320, emailTextView.contentSize.height + rightKeyboardSize.height + 20);
}
对这里出什么问题有任何想法吗? 提前致谢!
好的,我实施了自己的示例项目来找到答案。 您应该替换您的名字。
首先,我将添加观察者以检测何时显示或隐藏键盘:
- (void)addKeyboardObserver
{
// This could be in an init method.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardDidShow:(NSNotification*)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
_keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
UIScrollView *_scrollView = (UIScrollView*)self.view;
_scrollView.frame = CGRectMake(_scrollView.frame.origin.x,
_scrollView.frame.origin.y,
_scrollView.frame.size.width,
_scrollView.frame.size.height - _keyboardFrameBeginRect.size.height);
}
- (void)keyboardDidHide:(NSNotification*)notification
{
UIScrollView *_scrollView = (UIScrollView*)self.view;
_scrollView.frame = CGRectMake(_scrollView.frame.origin.x,
_scrollView.frame.origin.y,
_scrollView.frame.size.width,
_scrollView.frame.size.height +
_keyboardFrameBeginRect.size.height);
}
然后,将textViewDidChange:方法更改为:
- (void)textViewDidChange:(UITextView *)textView
{
UIScrollView *_scrollView = (UIScrollView*)self.view;
_textView.frame = CGRectMake(_textView.frame.origin.x,
_textView.frame.origin.y,
_textView.contentSize.width,
_textView.contentSize.height);
_scrollView.contentSize = _textView.frame.size;
if (_scrollView.frame.size.height < _textView.frame.size.height) {
CGPoint bottomOffset = CGPointMake(0,_textView.frame.size.height-_keyboardFrameBeginRect.size.height);
[_scrollView setContentOffset:bottomOffset animated:NO];
}
}
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.