我得到一個問題是,我想在鍵盤顯示時向上移動我的UIView(footerview),並在鍵盤被解散時將其移下。如何在鍵盤出現或關閉時移動包含在UIViewController中的UIView?
- 我的UIView(FooterView)包含在由Xcode自動生成的Main.Storyboard中的UIViewController中。
- 我也有一個TextField。
視圖層次的樣子:
查看:
- >文本字段
- >的UIView(FooterView)
編輯:
後我發佈這個問題,我發現我自己的回答
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Assign new frame to view
[self.footerView setFrame:CGRectMake(0,250,320,65)];
}
-(void)keyboardDidHide:(NSNotification *)notification
{
// set it back to the original place
[self.footerView setFrame:CGRectMake(0,503,320,65)];
}
後你有什麼到目前爲止已經試過。 – atulkhatri
你想在文本框輸入時移動視圖 –
是的,我想上下移動它。 – vichhai