objective-C UITextField Positioning

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
I have a login screen in my app on which I hide UITextFields depending on whether it's the first time the user has logged in.

I have 5 fields in total, and the middle 3 get hidden. In this instance, I want to move the bottom field up so that it sits where the second used to (i.e. underneath the first).

I'd been doing this by setting the center attribute of the bottom field to be the same as the second in ViewDidLoad - this worked fine but has suddenly stopped and I'm not sure what I've done (if anything) to cause this.

Any ideas, or better ways to do this?
 
You can do this by setting the frame of the UITextField to the position and size you want with CGRectMake. If you want to animate the movement, put it in an animation block.

myTextField.frame = CGRectMake(newXPos, newYPos, myTextField.frame.size.width, myTextField.frame.size.height);
 
Last edited:
This should work:

SecondTextField.frame = CGRectMake(firstTextField.frame.origin.x,firstTextField.frame.origin.x,secondTextField.frame.size.width,secondTextField.size.height)

This is off the top of my head (on iPad) but all you are doing is setting the frame of the uitextfield you want to move to the x and y of the origin of the first uitextfield, while keeping the 2nds width and height.

Not able to try it but you could also try

secondTextField.frame = firstTextField.frame

If the have the same width and height.
 
At first I had secondTextField.center = firstTextField.center, which worked fine, but for some reason then stopped working.

I've also tried the CGRectMake method, which also didn't work, so I logged the values that we're being passed (as in your example) and they all returned zero??

I'll try the frame = frame version you mention at the bottom - they are both exactly the same dimensions. I literally need to move it up x number of pixels!
 
Yeah it's all connected, I know because I read the text input in another part of my code, I also hide the same firstTextField after I move the secondTextField to it's position so I know that bits working...strange
 
If you are returning 0 then probably not this but have you tried a setNeedsDisplay? Might be worthwhile changing the position of the UITextfield in a background thread.

Put a break point in and examine your UITextFields as you go through that section of code, also get some NSLogs in there as well to make sure.
 
I'm confused as it suddenly stopped working, after being fine for some time!

I've NSLogged the center values of both UITextFields and they are both zero??

I tried setting the frame too, that didn't work.

Where/how do I call setNeedsDisplay?

I'm sure I'm missing something simple somewhere, it's like I don't have them hooked up correctly, but I do as I can read the text entered by the user...
 
Could you post your code for that section?

The center values for the UITextFields can't be zero, must be an issue with how they are connected. You could delete the link with IB and drag a new link just to make sure.

These problems can be frustrating.
 
Here's my code:

Code:
if(sqlite3_step(statement) == SQLITE_ROW){
                
                aUserIsAlreadyRegistered = true;
                
                self.password.center = CGPointMake(self.password.center.x, self.surname.center.y);
                NSLog(@"New Password Center: %f, Surname Center: %f",self.password.center.y, self.surname.center.y);
                self.surname.hidden = true;
                self.dateofbirth.hidden = true;
                self.postcode.hidden = true;

A query runs to check for the existence of a 'users' table, and if there is one, the user has logged in before (I know that sounds presumptuous but that would be correct for my application - irrelevant anyway)

So if the users table does exist, set a flag to say so, and they set up the view. So I change the password textfield to move to the location of the surname textfield, and then hide the fields that aren't required.

At first I thought that it was to do with the If, but the fields get hidden correctly! The NSLog is where I'm seeing zero returned for both!
 
The fact you are getting 0 returned is strange when when you can access the text.

[self.view setNeedsDisplay] can be called to force a redraw of the UI but only if you have changed the drawRect method, it won't be that (was thinking aloud when I mentioned it).

Off to work but will have a think about it.
 
OK, I've tried creating a CGRect (from static values).

I've set this as the frame of the UITextField, but it still doesn't move.

The strange thing is, logging the new values of the UITextField frame returns the correct figures!

Really stumped on this one!
 
Back
Top Bottom