当我尝试以编程方式创建 11 个文本字段时,我遇到了困难。问题是文本字段不显示。我在 viewDidLoad 方法中创建它们。
这是我正在使用的代码:
- (void)viewDidLoad
{
// Determine some basic info
int numberOfTextfields = 11;
int textfieldHeight = 40;
int textfieldWidth = 200;
// Create the UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, numberOfTextfields*textfieldHeight,textfieldWidth)];
// Create all the textfields
NSMutableArray *textfields = [NSMutableArray arrayWithCapacity:
(NSUInteger)numberOfTextfields];
for(int i = 0; i < numberOfTextfields; i++) {
UITextField *field = [[UITextField alloc] initWithFrame:
CGRectMake(0,i*textfieldHeight,textfieldHeight,textfieldWidth)];
[scrollView addSubview:field];
[textfields addObject:field];
}
[super viewDidLoad];
}
有什么线索可以解释为什么他们没有出现吗?
提前致谢。
最佳答案
您似乎从未将 scrollView
添加为 subview 。
您的文本字段实际上是隐藏的,因为您从未设置边框样式。试试这个:
- (void)viewDidLoad
{
// Determine some basic info
int numberOfTextfields = 11;
int textfieldHeight = 40;
int textfieldWidth = 200;
// Create the UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, textfieldWidth,numberOfTextfields*textfieldHeight)];
scrollView.contentSize = CGSizeMake(numberOfTextfields*textfieldWidth, textfieldHeight);
// Create all the textfields
NSMutableArray *textfields = [NSMutableArray arrayWithCapacity:
(NSUInteger)numberOfTextfields];
for(int i = 0; i < numberOfTextfields; i++) {
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0,i*textfieldHeight,textfieldWidth,textfieldHeight)];
field.borderStyle = UITextBorderStyleRoundedRect;
[scrollView addSubview:field];
[textfields addObject:field];
}
[self.view addSubview:scrollView];
[super viewDidLoad];
}
关于iphone - 以编程方式创建 UITextField iphone 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6371413/