我試圖以編程方式創建一些文本字段。它們出現在屏幕的底部,當選擇鍵盤時它們覆蓋它們。當選擇文本框時,我需要將視圖向上移動。當UITextField被選中時移動UIView
這裏是我的視圖代碼:
頁眉:
@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中返回到正常狀態還需要什麼?
謝謝,但然後標識視圖不會轉移,所以如果視圖不包括標識視圖,你會看到重疊。無論如何移動整個視圖。我應該創建另一個變量是整個視圖,並更新origin.y而不是? – StuartM 2013-02-19 18:32:30
哦,我的錯誤 - 輕鬆修復。在textFieldDidBeginEditing方法中使用self.view.frame而不是fieldView.frame,這將移動整個視圖。 – TiernanKennedy 2013-02-19 21:56:33
這是否解決了您的問題? – TiernanKennedy 2013-02-21 19:51:45