Reputation: 2781
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
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
Reputation: 17170
Why not use the view's center property?
userNameField.center = parentViewField.center;
Upvotes: 1
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