Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 387
vivianaranha Avatar asked Oct 07 '10 22:10

vivianaranha


2 Answers

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

usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 200, 28)];
usernameField.center = myView.center;
like image 187
pablasso Avatar answered Oct 01 '22 10:10

pablasso


userNameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 28)];
userNameField.center = CGPointMake(CGRectGetWidth(someView.bounds)/2.0f, CGRectGetHeight(someView.bounds)/2.0f);

Where someView is the superview of userNameField.

like image 33
Joshua Weinberg Avatar answered Oct 01 '22 10:10

Joshua Weinberg