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