UITextField in UIAlertView on iPhone - how to make it responsive?

前端 未结 10 1408
情歌与酒
情歌与酒 2020-11-28 05:27

I placed a UITextField into a UIAlertView and moved it up so the keyboard wouldn\'t cover it up with the following code:

[dialog setDelegate:self];
[dialog s         


        
10条回答
  •  佛祖请我去吃肉
    2020-11-28 06:05

    A quite nice approach is, you can use the delegate methods of the UITextFieldDelegate to move the dialog up only when the keyboard is activated. See below.

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5]; 
        [UIView setAnimationDelegate:self];
    
        CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, -20);
        [dialog setTransform: moveUp];
    
        [UIView commitAnimations];
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5]; 
        [UIView setAnimationDelegate:self];
    
        CGAffineTransform moveDown = CGAffineTransformMakeTranslation(0.0, 0.0);
        [dialog setTransform: moveDown];
    
        [textField resignFirstResponder];
    
        return YES;
    }
    

提交回复
热议问题