How can I create more than 40-50 text boxes and labels in one view?

How can I create more than 40-50 textboxes and labels in one view, and when a textbox is selected, the keyboard should not hide the textbox?

0


a source to share


1 answer


You probably want to create them programmatically - using Interface Builder it will be quite time consuming to create 40-50 text fields.

As for the keyboard, you can make your main UIView scrollable, then whenever the keyboard is displayed, check which textbox is selected and scroll to the top half of the screen. (If your app rotates, make sure the "top half of the screen" changes definitions based on your orientation.)

Sample code for this idea:



// Determine some basic info
int numberOfTextfields = 50;
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];
}

      

In this code, we first set some variables that determine the behavior of the text boxes (position, appearance, and number) and then create the UIScrollView wizard. After that, we create a bunch of UITextFields with the dimensions specified previously, while simultaneously adding them as scroll pods and holding them in an array for later reference (if needed).

Later, you will want to override the method becomeFirstResponder:

for your UITextFields (perhaps subclassing UITextField here) so that whenever the textbox becomes the first responder and shows the keyboard, it calls setContentOffset:animated:

in the scrollbar to display itself.

+1


a source







All Articles