gpt4 book ai didi

ios - 选择 UITextField 时向上移动 UIView

转载 作者:行者123 更新时间:2023-12-01 16:52:03 25 4
gpt4 key购买 nike

我正在尝试以编程方式创建一些文本字段。它们出现在屏幕底部,选择时键盘覆盖它们。选择文本字段时,我需要向上移动 View 。

这是我的 View 代码:

标题:

@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];

我已添加此方法来移动 View ,但无法正确计算最后一部分:
- (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];

}

我真正想要的是完全改变 View ,或者可能将其更改为上方的可滚动区域。尽管我在触摸到 endEditing 后确实实现了另一种方法。

似乎如果我移动一个字段,我必须完成代码才能移动所有......我怎样才能移动 View (完整 View )?

此外,在 textFieldDidEndEditing|textFieldShouldEndEditing 方法中 View 返回正常状态还需要什么?

最佳答案

当您在键盘被选中后制作动画时 - 改为为整个 View 制作动画。

首先声明 UIView 变量以使其可访问:

@interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> 
{
IBOutlet UITextField *usernameField;
UIView *fieldView;
}

然后在您的实现中创建此 View
 // 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
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];

然后为 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];

fieldView.frame = CGRectMake(fieldView.frame.origin.x,
-200,
fieldView.frame.size.width,
fieldView.frame.size.height);

[UIView commitAnimations];

}

由于所有文本字段都是 fieldView 的 subview - 它们将使用它进行动画处理。那应该排序!

关于ios - 选择 UITextField 时向上移动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14858701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com