所以......当键盘弹出时,我的UIScrollView会向上移动,这样可行...除了UIScrollView和键盘没有同步...首先,键盘弹出,然后UIScrollView。
我知道有一种方法可以延迟键盘,使其在视图向上滚动的同时显示出来;我怎么做??我在viewDidLoad中试过这个:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
......我也有这个:
- (void)keyboardWillShow:(NSNotification *)notification
{
[NSTimer scheduledTimerWithTimeInterval:4000 target:self selector:@selector(keyboardWillShow:) userInfo:nil repeats:NO];
}
(我知道,4000是一个巨大的数字,但我想确保有一个延迟!!)
此外,当我关闭键盘而不是平滑滚动时,UIScrollView只是简单地跳回原位而不是放松...是否有合理的方法来处理它?</ p>
更新:
知道了......感谢Steven Fisher在正确的道路上帮助我...我将所有内容都移到了keyboardWillShow,并添加了以下代码:
[UIScrollView beginAnimations:nil context:NULL];
[UIScrollView setAnimationDelegate:self];
[UIScrollView setAnimationDuration:.32];
[UIScrollView setAnimationBeginsFromCurrentState:NO];
当键盘消失时,这也解决了我的“跳跃”问题!哇噢!
答案 0 :(得分:0)
这是苹果文档为此类情况提供的代码。
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
答案 1 :(得分:0)
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification *)notification
{
// To avoid keyboard hides the view
CGRect frame = self.view.bounds;
if (capitalTextField.enabled ==YES)
{
if ([notification name]== UIKeyboardDidShowNotification )
{
frame.origin.y += 200;
[self.scrollView scrollRectToVisible:frame animated:YES];
}
else
{
frame.origin.y -= 200;
[self.scrollView scrollRectToVisible:frame animated:YES];
}
}
}
答案 2 :(得分:0)
以下代码会延迟弹出
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[textField becomefirstresponder];
[UIView commitAnimations];