我正在尝试以编程方式创建一些文本字段。它们出现在屏幕底部,选择时键盘覆盖它们。选择文本字段时,我需要向上移动视图。
这是我的视图代码:
标题:
@interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> {
IBOutlet UITextField *usernameField;
}
小鬼查看文件:
// Create sub view for Logo
UIView *logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,320,280)];
[logoView setBackgroundColor:[UIColor blueColor]];
logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
// Create sub view for fields
UIView *fieldView = [[UIView alloc] initWithFrame:CGRectMake(0, logoView.bounds.size.height, 320, 200)];
[fieldView setBackgroundColor:[UIColor greenColor]];
fieldView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
// Setting up the text fields
// Username field
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
usernameField.borderStyle = UITextBorderStyleRoundedRect;
usernameField.textColor = [UIColor blackColor];
usernameField.font = [UIFont systemFontOfSize:17.0];
usernameField.placeholder = @"Username";
usernameField.backgroundColor = [UIColor whiteColor];
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.returnKeyType = UIReturnKeySearch;
usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameField.delegate = self;
// Password field
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(10, (usernameField.bounds.size.height + 30), 300, 40)];
passwordField.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
passwordField.borderStyle = UITextBorderStyleRoundedRect;
passwordField.textColor = [UIColor blackColor];
passwordField.font = [UIFont systemFontOfSize:17.0];
passwordField.placeholder = @"Password";
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.autocorrectionType = UITextAutocorrectionTypeNo;
passwordField.keyboardType = UIKeyboardTypeDefault;
passwordField.returnKeyType = UIReturnKeySearch;
passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordField.delegate = self;
// Add fields/button to fieldView
[fieldView addSubview:usernameField];
[fieldView addSubview:passwordField];
// Add subviews to main view
[self.view addSubview:logoView];
[self.view addSubview:fieldView];
我已添加此方法来移动视图,但无法正确计算最后一部分:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing");
//If we begin editing on the text field we need to move it up to make sure we can still
//see it when the keyboard is visible.
//I am adding an animation to make this look better
[UIView beginAnimations:@"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
usernameField.frame = CGRectMake(usernameField.frame.origin.x,
-200,
usernameField.frame.size.width,
usernameField.frame.size.height);
[UIView commitAnimations];
}
我真正想要的是完全改变视图,或者可能将其更改为上方的可滚动区域。尽管我在触摸到 endEditing 后确实实现了另一种方法。
似乎如果我移动一个字段,我必须完成代码才能移动所有......我怎样才能移动视图(完整视图)?
此外,在 textFieldDidEndEditing|textFieldShouldEndEditing 方法中视图返回正常状态还需要什么?