2015-01-06 20 views
0

我有一个问题,其中键盘解雇,我认为干扰了我的动画代码。我想要发生的情况是,如果用户在键盘启动时单击搜索按钮,则键盘会掉落,文本字段会在屏幕顶部生成动画。 喜欢在这个图像。如何停止iOS键盘干扰UITextField位置

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

我只是想找到最好的解决方案来解决这个问题,或者可能重新设计类似的东西。 任何帮助将不胜感激! :)

+0

^h你试图用自动布局约束来做到这一点?您可以在键盘框发生变化时得到通知,以便根据需要调整约束来移动元素。 – myte

+0

@myte我认为我最近看到了,我想过但是我不太确定如何实际执行,不得不多看看这个主题 – Dowling1dow

回答

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

     }]; 
    }]; 
+1

你是上帝! 非常感谢你,我在那里发布了一个解决方案,在键盘编辑过程中我只是删除了搜索按钮,但这正是我想要的! 谢谢! – Dowling1dow

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

嘿,男人非常感谢你的帮助,这似乎实施起来有点麻烦,但你的想法确实帮助我发现了一个更简单的解决方案! 我会将其作为他人看到的解决方案发布。 非常感谢您花时间帮助!非常感谢 – Dowling1dow