2015-04-02 82 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。

相關問題