在 iOS 中以编程方式在另一个文本字段下方添加文本字段
Posted
技术标签:
【中文标题】在 iOS 中以编程方式在另一个文本字段下方添加文本字段【英文标题】:Adding textfield below another textfield programmatically in iOS 【发布时间】:2015-04-02 12:44:37 【问题描述】:在我的应用程序中,如果需要单击按钮,我想以编程方式在另一个下方添加文本字段。我已经提供了两个文本字段。如果用户想要添加另一个文本字段,他可以通过单击按钮来实现。我已经编写了获取文本字段的代码,但问题是它与已经设计的文本字段重叠。我该怎么做?
有什么方法可以获取已设计文本字段的 x 和 Y 坐标,以便我可以相对于这些坐标放置新的文本字段。
【问题讨论】:
首先,贴出你的代码,让我们看看你说的问题。其次,是的,有可能,请谷歌 UIView 属性frame
。第三,你应该学习 autoLayout,它更难,但从长远来看是值得的
【参考方案1】:
此代码添加文本字段以在按钮上的每次单击操作时动态查看
ExampleViewController.h
#import <UIKit/UIKit.h>
@interface ExampleViewController :UIViewController<UITextFieldDelegate>
@property int positionY;
@property int fieldCount;
@property (strong,nonatomic) UIScrollView *scroll;
@end
ExampleViewController.m
#import "ExampleViewController.h"
@interface ExampleViewController ()
@end
@implementation ExampleViewController
@synthesize positionY;
@synthesize fieldCount;
@synthesize scroll;
- (void)viewDidLoad
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.backgroundColor = [UIColor whiteColor];
[self.view addSubview:scroll];
UIButton *clickToCreateTextField = [[UIButton alloc] initWithFrame:CGRectMake(40, 80, self.view.frame.size.width-80, 75)];
[clickToCreateTextField setTitle:@"Create Text Field" forState:UIControlStateNormal];
[clickToCreateTextField addTarget:self action:@selector(clickedButton) forControlEvents:UIControlEventTouchUpInside];
[clickToCreateTextField setBackgroundColor:[UIColor blackColor]];
[scroll addSubview:clickToCreateTextField];
positionY = clickToCreateTextField.center.y;
fieldCount = 0;
// Do any additional setup after loading the view.
-(void) clickedButton
//add text field programmitacally
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, positionY, self.view.frame.size.width-80, 75)];
textField.delegate = self;
//give a tag to determine the which textField tapped
textField.tag = fieldCount;
textField.placeholder = [NSString stringWithFormat:@"Your dynamically created textField: %d", fieldCount ];
[scroll addSubview:textField];
//check if the textFields bigger than view size set scroll size and offset
if (positionY>= self.view.frame.size.height)
scroll.contentOffset = CGPointMake(0, positionY);
scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height+positionY);
fieldCount++;
//increase the position with a blank place
positionY = positionY+textField.frame.size.height+20;
#pragma mark TextField Delegate Methods
-(void) textFieldDidBeginEditing:(UITextField *)textField
//Do what ever you want
-(void) textFieldDidEndEditing:(UITextField *)textField
[textField resignFirstResponder];
//do anything
-(BOOL) textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];
return YES;
-(void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
您可以对此代码进行任何其他更改。 我认为这个例子解释了你的答案。
希望对你有帮助。
【讨论】:
【参考方案2】:使用计数器并计算 y,如 counter*texfield.frame.size.height。
【讨论】:
以上是关于在 iOS 中以编程方式在另一个文本字段下方添加文本字段的主要内容,如果未能解决你的问题,请参考以下文章