可能重复:
Making the view slide up to make room for the keyboard?
Xcode/iOS5: Move UIView up, when keyboard appears
正如我们通常在文本字段中输入数据键盘一样,它会隐藏数据我们在字段中输入的内容,因此屏幕应该向上滑动以便我们可以看到在fiedl中输入的数据。
答案 0 :(得分:1)
试试这段代码.......
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= moveKeyboard;
}
else
{
rect.origin.y += moveKeyboard;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void)keyboardWillShow
{
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide
{
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
编辑:moveKeyboard是浮动的。根据您的需要设定其值。
答案 1 :(得分:0)
有通知(UIKeyboard[Will|Did][Show|Hide]Notification
)告诉您键盘即将出现或消失,您可以使用它们来触发移动视图的代码。移动视图的方法有多种 - 您可以自己移动视图,根据需要调整位置;你可以将它们全部放在一个视图中,这样你只需要移动容器;或者您可以将它们嵌入滚动视图中,只需调整滚动视图的内容偏移量。
请参阅Apple的文档Managing the Keyboard,特别是名为Moving Content That is Located Under the Keyboard的部分。那里也有示例代码,而且效果很好。