2

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.

1
  • Where do you put the scrollview itself? Commented Jun 16, 2011 at 12:07

5 Answers 5

5

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];
}
Sign up to request clarification or add additional context in comments.

4 Comments

i tried [self.view addSubview:scrollView]; but it didn't work that's why i removed it from the code.
border style... ok thanks. One question: how can i separate the textfields from eachother?
Just add some padding. When you create your UITextField, do this: UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0, i*textfieldHeight+10, textfieldWidth, textfieldHeight)];. Be sure to increase the contentSize of the scroll view appropriately.
that's exactly what i tried and it didn't work, i only got padding before the first field not in between them. i guess i will make a new question about this when i give up searching
0

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

Comments

0

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]

Comments

0

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];

Comments

0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.