Reputation: 4431
I'm struggling when i try to create 11 text fields programmatically. The problem is that the text fields doesn't show up. I'm creating them in the viewDidLoad
method.
Here's the code i'm using:
- (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];
}
Any clues on why they don't show up?
Thanks in advance.
Upvotes: 2
Views: 5749
Reputation: 2314
Try the below code. Textfields are added and you can scroll the textview.
int numberOfTextfields = 11;
int textfieldHeight = 40;
int textfieldWidth = 200;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,textfieldWidth,numberOfTextfields*textfieldHeight)]; scrollView.contentSize = CGSizeMake(textfieldWidth, numberOfTextfields*textfieldWidth+10);
NSMutableArray *textfields = [NSMutableArray arrayWithCapacity: (NSUInteger)numberOfTextfields];
for(int i = 1; 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];
Upvotes: 0
Reputation: 29524
You seem to never add scrollView
as a subview.
Your textFields were atually hidden because you never set the border style. Try this:
- (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];
}
Upvotes: 5
Reputation: 453
Check this code.....frame parameters are(x,y,width,height)
int numberOfTextfields = 11;
int textfieldHeight = 40;
int textfieldWidth = 200;
// Create the UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,textfieldWidth,numberOfTextfields*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];
Upvotes: 0
Reputation: 2547
you need to add your scroll view to your view controller. if it is allocated,
else do this
self.view = [[UIView alloc] initWithFrame:CGRectMake (0,0,320,460)]
add this line at the end [self.view addSubview: scrollView]
Upvotes: 0
Reputation: 8583
You need to add your UIScrollView
to your view and to setup its content area, as it doesn't manage it automagically (as you might have expected).
Upvotes: 0