如何阻止iOS键盘干扰UITextField位置

时间:2015-01-06 15:10:57

标签: ios objective-c iphone keyboard

我遇到键盘解雇的问题,我认为干扰了我的动画代码。我想要发生的是,如果用户在键盘启动时单击搜索按钮,键盘将会下降,文本字段将显示在屏幕顶部。 就像在这张图片中一样。

http://imgur.com/5dQ6aPZ

但是,如果在键盘启动时按下搜索按钮,实际发生的情况是这样的。

http://imgur.com/tp6Ffne

以下是搜索按钮的代码:

- (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];
}

我只是想找到解决这个问题的最佳解决方案,或者重新设计类似的东西。 非常感谢任何帮助! :)

2 个答案:

答案 0 :(得分:0)

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];

答案 1 :(得分:0)

替换您的代码如下: 希望它现在可以使用

搜索按钮:

- (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];

        }];
    }];