Search code examples
iphoneuitextfieldalignment

Placing a UITextField in the center of the view


I have a UITextField and I want to center this field on the center of the view. Any ideas?

usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 200, 28)];

Instead of CGRectMake(some numbers) - I want to place it in the center of the view.


Solution

  • You can directly assign it the center of the desired view:

    usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 200, 28)];
    usernameField.center = myView.center;