您要设置autoresizingMask
视图的属性。
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];
这也可以从 Interface Builder 完成。甚至还有一个小动画展示了如何在 Interface Builder 中调整视图的大小。