当键盘出现在像 4s 、 5 和 5s 这样的小屏幕上时如何向上移动视图但是当它进入 iphone 6 时视图不应该向上移动
How to move View up when Keyboard appeared in small screen like 4s , 5 and 5s but view should not move up when its enter inside iphone 6
如何在 4s、5 和 5s 等小屏幕上出现键盘时向上移动视图,但在进入 iphone 6 和 6S 时视图不应向上移动。
我正在分享我的代码,这是我在我的应用程序中为所有屏幕完成的,但在我的情况下,当它在 6 或 6 秒内输入时,视图不应向上移动。
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
const int movementDistance = -60; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
我建议你使用IQKeyboardManager它会为你管理一切事半功倍
// 在主视图上添加一个滚动视图并在该滚动视图上添加 UITextField
-(void) viewDidLoad
{
UIScrollView *myScrollView = [ [UIScrollView alloc] initWithFrame:[UIScreen mainScreen ].bounds];
[myScrollView .contentSize =CGSizeMake(320, 500);
[myScrollView .contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
[self.view addSubView:myScrollView ];
UITextField *myTextField = [ [UITextField alloc] initWithFrame:CGRectMake(20,30,100,33)];
[myScrollView addSubView:myTextField ];
myTextField.delegate = self;
}
//设置scrollview内容偏移量使myTextField上移
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[myScrollView setContentOffset:CGPointMake(0,textField.center.y-80) animated:YES];
// here '80' can be any number which decide the height that textfiled should move
}
//将文本框移动到原来的位置
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[[myScrollView setContentOffset:CGPointMake(0,0) animated:YES];
[textField resignFirstResponder];
return YES;
}
如何在 4s、5 和 5s 等小屏幕上出现键盘时向上移动视图,但在进入 iphone 6 和 6S 时视图不应向上移动。
我正在分享我的代码,这是我在我的应用程序中为所有屏幕完成的,但在我的情况下,当它在 6 或 6 秒内输入时,视图不应向上移动。
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
const int movementDistance = -60; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
我建议你使用IQKeyboardManager它会为你管理一切事半功倍
// 在主视图上添加一个滚动视图并在该滚动视图上添加 UITextField
-(void) viewDidLoad
{
UIScrollView *myScrollView = [ [UIScrollView alloc] initWithFrame:[UIScreen mainScreen ].bounds];
[myScrollView .contentSize =CGSizeMake(320, 500);
[myScrollView .contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
[self.view addSubView:myScrollView ];
UITextField *myTextField = [ [UITextField alloc] initWithFrame:CGRectMake(20,30,100,33)];
[myScrollView addSubView:myTextField ];
myTextField.delegate = self;
}
//设置scrollview内容偏移量使myTextField上移
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[myScrollView setContentOffset:CGPointMake(0,textField.center.y-80) animated:YES];
// here '80' can be any number which decide the height that textfiled should move
}
//将文本框移动到原来的位置
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[[myScrollView setContentOffset:CGPointMake(0,0) animated:YES];
[textField resignFirstResponder];
return YES;
}