如何停止 iOS 键盘干扰 UITextField 位置
How to stop iOS keyboard interfering with UITextField position
我遇到键盘关闭的问题,我认为它干扰了我的动画代码。我想要发生的是,如果用户在键盘启动时单击搜索按钮,键盘会掉落并且文本字段将动画显示到屏幕顶部。
就像这张图片。
但是,如果在键盘弹起时按下搜索按钮,实际发生的情况是这样的。
这是 搜索按钮的代码:
- (IBAction)searchButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x - 0.0, (_roomCodeInput.frame.origin.y - 105.0), _roomCodeInput.frame.size.width + 0.0, _roomCodeInput.frame.size.height);
[UITextField commitAnimations];
}
“再次搜索”按钮
- (IBAction)searchAgainButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.6
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = NO;
[_srcButton setHidden:NO];
[_srcAgainButton setHidden:YES];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x + 0.0, (_roomCodeInput.frame.origin.y + 105.0), _roomCodeInput.frame.size.width - 0.0, _roomCodeInput.frame.size.height + 0.0);
[UITextField commitAnimations];
}
下面是我正在使用的方法关闭键盘
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[_roomCodeInput resignFirstResponder];
return NO;
}
-(void)dismissKeyboard{
[_roomCodeInput resignFirstResponder];
}
我只是想找到解决这个问题的最佳解决方案,或者重新设计类似的东西。
任何帮助将不胜感激! :)
NSArray * vertConstraint;
UIButton * button;
UITextField * textField;
-(void)search{
[self.view removeConstraints:vertConstraint];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(button, textField);
NSArray * newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[textField(==30)]-(>=10)-[button]-10-|" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:newVertConstraint];
[textField resignFirstResponder];
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
vertConstraint = newVertConstraint;
}
self.automaticallyAdjustsScrollViewInsets = NO;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Search" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
textField = [[UITextField alloc] init];
textField.layer.borderWidth = 5;
textField.layer.borderColor = [UIColor redColor].CGColor;
[self.view addSubview:textField];
[textField setTranslatesAutoresizingMaskIntoConstraints: NO];
[button setTranslatesAutoresizingMaskIntoConstraints: NO];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(button, textField);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[textField]-20-|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[button]-20-|" options:0 metrics: 0 views:viewsDictionary]];
vertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[textField(==30)]-10-[button]" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:vertConstraint];
[textField becomeFirstResponder];
按如下方式替换您的代码:
希望现在能用
搜索按钮:
- (IBAction)searchButton:(id)sender {
//[UITextField commitAnimations];
[UIView animateWithDuration:0.5 animations:^{
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
_roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, -100.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
}];
}];
}
“再次搜索”按钮
- (IBAction)searchAgainButton:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
_enterRoomLabel.hidden = NO;
[_srcAgainButton setHidden:YES];
_roomCodeInput.text=Nil;
_roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, 0.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
[_srcButton setHidden:NO];
}];
}];
我遇到键盘关闭的问题,我认为它干扰了我的动画代码。我想要发生的是,如果用户在键盘启动时单击搜索按钮,键盘会掉落并且文本字段将动画显示到屏幕顶部。 就像这张图片。
但是,如果在键盘弹起时按下搜索按钮,实际发生的情况是这样的。
这是 搜索按钮的代码:
- (IBAction)searchButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x - 0.0, (_roomCodeInput.frame.origin.y - 105.0), _roomCodeInput.frame.size.width + 0.0, _roomCodeInput.frame.size.height);
[UITextField commitAnimations];
}
“再次搜索”按钮
- (IBAction)searchAgainButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.6
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = NO;
[_srcButton setHidden:NO];
[_srcAgainButton setHidden:YES];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x + 0.0, (_roomCodeInput.frame.origin.y + 105.0), _roomCodeInput.frame.size.width - 0.0, _roomCodeInput.frame.size.height + 0.0);
[UITextField commitAnimations];
}
下面是我正在使用的方法关闭键盘
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[_roomCodeInput resignFirstResponder];
return NO;
}
-(void)dismissKeyboard{
[_roomCodeInput resignFirstResponder];
}
我只是想找到解决这个问题的最佳解决方案,或者重新设计类似的东西。 任何帮助将不胜感激! :)
NSArray * vertConstraint;
UIButton * button;
UITextField * textField;
-(void)search{
[self.view removeConstraints:vertConstraint];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(button, textField);
NSArray * newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[textField(==30)]-(>=10)-[button]-10-|" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:newVertConstraint];
[textField resignFirstResponder];
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
vertConstraint = newVertConstraint;
}
self.automaticallyAdjustsScrollViewInsets = NO;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Search" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
textField = [[UITextField alloc] init];
textField.layer.borderWidth = 5;
textField.layer.borderColor = [UIColor redColor].CGColor;
[self.view addSubview:textField];
[textField setTranslatesAutoresizingMaskIntoConstraints: NO];
[button setTranslatesAutoresizingMaskIntoConstraints: NO];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(button, textField);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[textField]-20-|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[button]-20-|" options:0 metrics: 0 views:viewsDictionary]];
vertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[textField(==30)]-10-[button]" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:vertConstraint];
[textField becomeFirstResponder];
按如下方式替换您的代码: 希望现在能用
搜索按钮:
- (IBAction)searchButton:(id)sender {
//[UITextField commitAnimations];
[UIView animateWithDuration:0.5 animations:^{
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
_roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, -100.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
}];
}];
}
“再次搜索”按钮
- (IBAction)searchAgainButton:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
_enterRoomLabel.hidden = NO;
[_srcAgainButton setHidden:YES];
_roomCodeInput.text=Nil;
_roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, 0.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
[_srcButton setHidden:NO];
}];
}];