vivianaranha
vivianaranha

Reputation: 2781

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.

Upvotes: 5

Views: 4332

Answers (3)

pablasso
pablasso

Reputation: 2499

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

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

Upvotes: 8

Rog
Rog

Reputation: 17170

Why not use the view's center property?

userNameField.center = parentViewField.center;

Upvotes: 1

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

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.

Upvotes: 2

Related Questions