Iphone Autoscroll UIScrollView以适应本机iOS Mail.app中的内容
原生iOSIphone Autoscroll UIScrollView以适应本机iOS Mail.app中的内容,iphone,ios,objective-c,cocoa-touch,uiscrollview,Iphone,Ios,Objective C,Cocoa Touch,Uiscrollview,原生iOSMail.app在创建新信件时具有强大的功能。整个屏幕是一个UIScrollView,而书写信件正文的位置是一个UITextView,滚动被禁用 键入此TextView的高度以及UIScrollView的高度会动态更改,并且UIScrollView会向下滚动,在键盘上方留下一些像素以显示新文本 我知道这一过程必须在textViewDidChange方法中完成,但在尝试这样做的同时,我的代码中出现了一些错误-UITextField有时可能会在UIScrollView下关闭。以下是我如何
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];
}
}
-(无效)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=\u textView.frame.size;
如果(_scrollView.frame.size.height<_textView.frame.size.height){
CGPoint bottomOffset=CGPointMake(0,_textView.frame.size.height-_keyboardFrameBeginRect.size.height);
[_ScrollViewSetContentOffset:bottomOffset动画:否];
}
}
祝你好运 好的,我实施了自己的示例项目来找到答案。你应该替换你的名字 首先,我将添加观察者来检测键盘何时显示或隐藏:
- (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];
}
}
-(无效)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=\u textView.frame.size;
如果(_scrollView.frame.size.height<_textView.frame.size.height){
CGPoint bottomOffset=CGPointMake(0,_textView.frame.size.height-_keyboardFrameBeginRect.size.height);
[_ScrollViewSetContentOffset:bottomOffset动画:否];
}
}
祝你好运 您的方法仅在UITextView可滚动时有效,而在禁用其滚动时无效。另外,我还有一个需要滚动的UIScrollView。UIScrollView有屏幕的大小?不,它的大小取决于UINavigationBar,并且是可自定义的,非常感谢!我一定会尝试一下,并报告结果!我在您的方法中上载示例项目,仅当UITextView可滚动时有效,而不是当其滚动被禁用时有效。另外,我还有一个需要滚动的UIScrollView。UIScrollView有屏幕的大小?不,它的大小取决于UINavigationBar,并且是可自定义的,非常感谢!我一定会尝试一下,并报告结果!我将示例项目上传到