Keyboard hides UITextField. How to solve this?

ε祈祈猫儿з 提交于 2019-12-10 10:58:37

问题


I have a textField which is at bottom of the screen. When I begin editing, the keyboard appears and hides the textField.

Can anyone suggest how to solve this?


回答1:


Generally you would put your content in a scroll view and move your scroll view up when the keyboard appears. There are many tutorials out there so google them. Here is one of them.




回答2:


You can use following code in two diffrent method, call it when editing began and end, to move your view up and down,(just change the valuew that is been subtracted in yourView.frame.origin.y-100 to adjust according to your need)

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.5];      
    yourView.frame = CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y-100, yourView.frame.size.width, yourView.frame.size.height);

    [UIView commitAnimations];



回答3:


See this code snippet. And use one UIButton to dismiss UIKeyboard.

- (void) animateTextField: (UITextField *)textField up:(BOOL) up
{
const int movementDistance = 200;
const float movementDuration = 0.4f;

int movement = (up ? -movementDistance : movementDistance);

up ? (self.button.enabled= YES) : (self.button.enabled = NO);

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}

-(void)textFieldDidBeginEditing:(UITextField*) inTextField
{
[self animateTextField:mText up:YES];
[self addObservers];
}

- (void)addObservers 
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:@"UITextFieldTextDidChangeNotification" object:nil];    
}

- (void)textFieldDidChange:(NSNotification*)aNotification 
{
   NSLog(@"TextField data=%@",mText.text);
}   

- (IBAction) doneBtn:(id)sender
{
[mText resignFirstResponder ];
[self animateTextField:mText up:NO];
}



回答4:


When you tap on textField keyboard up then your textField hide by it. so waht you need, you need to make your view up not keyboardresign is a solution. so for making you view up use

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField  delegate for the logic. now see the code

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    if(textField.tag==1)  //add tag to your textField to identify it
    {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];

        CGRect viewFrame=self.view.frame;
        viewFrame=sinViewFrame.size.height+100;
        viewFrameorigin.y-=100; // as required
        self.view.frame=viewFrame;
        [UIView commitAnimations];

   }

}

hope it helps you.




回答5:


I use auto layout constraint to solve this problem. I hold constraint as a property, and play with it when keyboard appears / disappears. I simply move views above the textfield out of screen, and textfield to the top. After finishing edit, I move them by the constraint to their original position.

-(void)keyboardWillShow:(NSNotification*)notification{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
    self.topSpaceLayoutConstraint.constant = -150;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
} completion:NULL];
}

-(void)keyboardWillHide:(NSNotification*)notification
{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
    self.topSpaceLayoutConstraint.constant = 10;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
} completion:NULL];
}

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpaceLayoutConstraint;


来源:https://stackoverflow.com/questions/6435670/keyboard-hides-uitextfield-how-to-solve-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!