2015-04-02 62 views
-1

在我的应用程序中,我想以编程方式将文本字段添加到另一个文本字段下方,如果需要点击一个按钮。我已经提供了两个textFields.if用户想要添加另一个文本框,他可以通过点击一个按钮来实现。我已经编写了代码来获取文本字段,但问题在于它与已设计的textField重叠。如何才能做到这一点。在另一个文本字段下方添加文本字段以编程方式在ios中

有没有什么办法可以让我得到已经设计好的textfield的x和y坐标,这样我就可以将新的textField放到相对于这些坐标的位置上。

+0

首先,发布你的代码,让我们看看你正在谈论的问题。其次,是的,这是可能的,请谷歌UIView属性“框架”。 第三,你应该学习autoLayout,它的强硬但是长期来看是值得的 – 2015-04-02 12:47:07

回答

0

该代码添加文本框,查看时动态按钮每次点击动作

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. 
} 

您可以对此代码进行任何其他更改。 我认为这个例子解释你的答案。

希望它有帮助。

0

使用计数器并计算y这个计数器* texfield.frame.size.height。

相关问题