views:

27

answers:

1

I have multiple textfields in a view (not a scrollview); 2 in particular are side by side, all are created in IB.

I use the same .xib for more than 1 entry screen. All textfields appear above the keyboard so keyboard covering these fields is NOT a problem.

On some screens I only need one of the two side-by-side textfields so I hide the other.

For aesthetic purposes only, on those screens where one field is hidden, I want to move the other textfield to the right or to the left in order to center that remaining textfield, keeping all of the other textfields on the screen stationery.

The .center property to get and move the center of the textfield, doesn't seem to work with uitextfield.

Any ideas please

A: 

.center should work on all UIView subclasses, including UITextField.
For example:

leftTextField.center = CGPointMake(100, 120);

Or:

leftTextField.center = CGPointMake(containingView.bounds.size.width / 2, containingView.bounds.size.height / 2);

You might also change the location by updating the .frame...

Could you post some code that you use and doesn't do the work?

EDIT: Modified the second code sample...

Michael Kessler
Thank you. I am now using textFieldLeft.center = CGPointMake(textFieldLeft.center.x + 75, textFieldLeft.center.y); and that works just fine. I was struggling with the textFieldLeft.center.x part. When you mentioned updating the frame, I thought of the .frame.origin.x syntax and figured that I'd try something similar and it worked. My previous attempts were compiling and running, just not moving the field. Thanks again.
Matt Winters
And based on your second code example, this works nicely too:textFieldLeft.center = CGPointMake(self.view.bounds.size.width / 2, textFieldLeft.center.y);
Matt Winters
Glad to help. BTW, have you figured out why your attempts didn't work? Maybe you tried `leftTextField.center.x = containingView.bounds.size.width / 2;` ? If this is the case then remember that in order to change the frame, bounds or center properties you have to change the entire struct - you can't modify only a part of it...
Michael Kessler