我正在使用以下代码来移动一个带有 uitextfield 的 uialertview。alertView 应该在键盘出现时向上滑动,并在它消失后立即向后滑动。以下代码在 ios 3.1.2 下对我来说工作得很好。但由于某种原因,它在 ios 4.0 下不起作用.....问题似乎是我正在进行的转换,但我不知道到底出了什么问题。如果有人知道解决方案,那就太好了!提前致谢!这是我的代码:
- (void)addItemAction{
workoutName = [[UIAlertView alloc] initWithTitle:@"New Workout" message:@"Insert the name of your new workout:\n " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
workoutName.cancelButtonIndex = 0;
UITextField *titleField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 90.0, 260.0, 25.0)];
titleField.delegate = self;
titleField.borderStyle = UITextBorderStyleRoundedRect;
titleField.returnKeyType = UIReturnKeyDone;
[workoutName addSubview:titleField];
[workoutName show];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -70.0);
[workoutName setTransform:myTransform];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 0.0);
[workoutName setTransform:myTransform];
[UIView commitAnimations];
if ([textField.text length] != 0) {
self.newWorkout = textField.text;
}
else {
self.newWorkout = @"";
}
}