我在 gpsnote 上做一个项目。如果有人有任何关于此应用程序的信息,请指导我...
我知道这段代码我希望文本字段应该与我的键盘一起出现
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
}
谢谢
首先将您的内容设置在滚动视图上,并在 viewDidLoad 中设置滚动视图:
[scrollView setContentSize : CGSizeMake (320, 1040)];
然后在 viewWillAppear 添加以下通知:
对于显示的键盘
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
用于键盘隐藏
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
以下是通知调用的两个函数
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
double keyboardHeight = kbSize.height;
double screenHeight = [UIScreen mainScreen].bounds.size.height - 20;
if(textOrigin > screenHeight - keyboardHeight)
{
double sofset = textOrigin - (screenHeight - keyboardHeight);
CGPoint offset = scrollBackGround.contentOffset;
offset.y += sofset;
[scrollBackGround setContentOffset:offset animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
[scrollBackGround setContentOffset:CGPointMake(0, 0) animated:YES];
}
在keyboardWasShown 函数中,我们所做的只是获取键盘的高度并检查textField y 轴(即函数中的textOrigin)是否大于键盘的Y 轴,而不是向上滑动包含我们的文本字段的滚动视图的内容。
现在如何获取文本字段 Y 轴。为此,您必须使用文本字段委托,以下委托将在您的文本字段成为第一响应者时触发
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textOrigin = scrollBackGround.frame.origin.y + textField.frame.origin.y + 20(it is status bar height) + yourNavigationBarheight;
// Make sure to make textOrigin an ivar in your .h file
}
最后在keyboardWillBeHidden中,我们正在重置滚动视图内容视图