將UIScrollView添加到您的Xib並創建IBOutlet連接,即添加您動態創建的所有標籤。 我在你的代碼中做了一些修改。它的工作正常。
-(void)setAnswerField
{
int len = 10;
int commonSpace = 20;
for (int i=0;i<=len;i++)
{
if(i != 0)
commonSpace += 40;
textF=[[UITextField alloc] initWithFrame:CGRectMake(((i * 60))+commonSpace,180,60,35)];
[textF setTag:i];
[textF setDelegate:self];
[textF setReturnKeyType:UIReturnKeyDone];
[textF setBackgroundColor:[UIColor grayColor]];
[textF addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
textF.clearButtonMode=UITextFieldViewModeWhileEditing;
[textF addTarget:self action:@selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
[_textfieldsScrollView addSubview:textF];
}
_textfieldsScrollView.contentSize=CGSizeMake(1000, 500);
}
在點擊文本字段textViewDidBeginEditing方法時,只需將內容從設置值更改爲這樣。
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[_textfieldsScrollView setContentSize:CGSizeMake(320, 400)];
[_textfieldsScrollView setContentOffset:CGPointMake(0, 100) animated:YES];
}
之後,恢復到原來的位置。
- (void)textViewDidEndEditing:(UITextView *)textView
{
//Back to normal state.
[_textfieldsScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
thanx男人的魅力! – koray