I started up an Xcode project using the ‘single view’ application template and added two lines to the template-created ViewController class in viewDidLoad:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(self.view.bounds, 10, 10)];
[self.view addSubview:textView];
When I run this in the 3.5 inch iPhone simulator, the textview extends off the bottom of the screen. I intended for it to be placed in the center of the screen with a 10 point border.
Am I missing something basic? It seems to work fine in the 4″ simulator. Is it a simulator bug?
It’s not a bug in the simulator.
The Xcode template includes a xib, and in that xib is the view controller’s top-level view (
self.view), and that view is sized for the iPhone 5 screen.When the system sends
viewDidLoadto your view controller, it hasn’t yet resized that view for the screen size of the current device. So your text view’s frame is based on the size of an iPhone 5 screen.You can fix this by setting the autoresizing flags on your view:
Your xib is set up to use autolayout by default, so you can also have the system resize your text view by setting constraints between your text view and your top-level view:
If you put your text view in the xib instead of creating it in code, you can use the xib editor (aka “Interface Builder” or “IB”) to set up the constraints. I highly recommend watching the autolayout videos from WWDC 2012: