0

I have a GUI class extending UIViewController.

In its function viewDidLoad, I would like to have a UITextField instance. I would like that instance not aligned as a simple CGRect, setting its offset and width and height, but I want it rather to be resized dynamically, making it fill the whole width of the screen, with an offset of 5px from top, left and right each and a height of 20px. How is that done?

It should maintain the "relatively-absolute" positioning regardless of the device orientation, all orientations being supported.

Maybe there's something like a LayoutManager, as, for instance, GridBagLayout in Java?

3 Answers 3

2

You want to set the autoresizingMask property of the view.

UIView *theParentView; // Assume this is the root view that owns the whole screen

CGRect textFieldFrame = CGRectMake(5, // Top-left corner x position
                                   5, // Top-left corner y position
                                   [theParentView bounds].width - 10, // Width
                                   20); // Height

UIView *myView = [[UIView alloc] initWithFrame:textFieldFrame];

// This will make it resize with the parent view, maintaining margins
[myView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

[theParentView addSubview:myView];

[myView release];

This can all be done from Interface Builder as well. There's even a little animation showing how the view will resize in Interface Builder.

0
1

Remember that a UIViewController itself isn't displayed, but it manages a hierarchy of view objects. You can create the text field in -viewDidLoad, but it needs to be added to the view controller's view before you can see it. You'd do something like this:

CGFloat inset = 5.0;
CGFloat fieldHeight = 20.0;
CGRect fieldRect = CGRectMake(inset,
                              inset,
                              self.view.size.width - (2 * inset),
                              fieldHeight);
UITextField *field = [[UITextField alloc] initWithFrame:fieldRect];
field.autoResizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:field];
[field release];

Of course, if you need to refer to that field again later, you'll also want to stash a pointer to it in a property or ivar.

2
  • Why the fieldHeight + inset for the y value in CGRectMake? That would put it at 25, but in UIKit you want the top-left corner, not the bottom-left.
    – BJ Homer
    Jun 3, 2011 at 20:26
  • @BJ Homer, good catch -- thanks. I wish they hadn't flipped the coordinate system in Cocoa Touch!
    – Caleb
    Jun 4, 2011 at 9:50
-1

There is nothing as such built into the OS. I have seen a couple of versions of something like what you're asking, including my own which is up on GitHub

2
  • 1
    Autoresizing masks will handle this particular use case just fine.
    – BJ Homer
    Jun 3, 2011 at 19:53
  • This is true, I didn't read the question as closely as I should have. But it does get closer to his second portion, about the Java Layout Managers. Jun 3, 2011 at 20:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.