UITextField won't center!

Discussion in 'iOS Programming' started by Parker21, Sep 19, 2014.

  1. Parker21 macrumors newbie

    Joined:
    Sep 19, 2014
    #1
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    I assume you mean that you want the text to be centered in the textfield.

    Just click the Center Alignment button in IB. Text Field/Alignment. It's the second one from the left. Leave that Control/Alignment stuff alone.
     
  3. Parker21, Sep 19, 2014
    Last edited by a moderator: Sep 19, 2014

    Parker21 thread starter macrumors newbie

    Joined:
    Sep 19, 2014
    #3
    All set.

    I have already set it to centre, when it's empty the place holder is centred but as soon as you enter content it goes left.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #4
    Pleas post your code as text.
    Please use CODE tags.

    It's easier to examine the code itself, rather than pictures of code.


    The picture of code you linked to shows the IBAction method forceCenter: using NSTextAlignmentRight. It was impractical to search the picture for the code that calls forceCenter:, so I have no idea if that code is ever executed or not. If it is, then where's the code that sets alignment to center?
     
  5. Parker21 thread starter macrumors newbie

    Joined:
    Sep 19, 2014
    #5
    Removed.

    All that code has since been removed, theres nothing but the text box, with the "middle" align highlighted.

    The only thing I have is this;
    Code:
    #import "RegisterViewController.h"
    
    @interface RegisterViewController ()
    
    @end
    
    @implementation RegisterViewController
    {
        int passwordRequired;
    }
    
    @synthesize businessName;
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    
    -(void)viewWillAppear:(BOOL)animated
    {
        
    }
    
    -(void)ReceiveNotification:(NSNotification *)notification
    {
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
    
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)toLogin:(id)sender {
        [self performSegueWithIdentifier:@"toLoginWindow" sender:self];
    }
    
    
    @end
    
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #6
    Your code has now been stripped down to the bare essentials. I see nothing obvious that would be affecting alignment.

    If the view is still misbehaving, then it's not in the code you posted. That leaves two logical possibilities where the problem could be:
    1. In some code you haven't posted.
    2. In the nib/xib itself.

    If there's no other code at all, then you'll probably have to post the complete compilable project that demonstrates the problem, so someone else can look at it. This means all the xibs, storyboards, or whatever resources are needed to build the project and run it.

    If you don't want to post the actual project, you'll need to make a simplified test case. Read this:
    http://forums.macrumors.com/showpost.php?p=19529649&postcount=4

    It's not uncommon that the process of making a simplified test case and systematically verifying behavior after every single change leads you to discover what's wrong with the original project. If it doesn't, then you at least have a postable test case that others can look at, edit, compile, and run to see the behavior.
     
  7. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    Just to be clear. No code is needed to center text in a UITextField. All you need to do is set the Alignment to center in IB and it should work as expected.

    If you've messed with other things to try to center the text you might want to remove that UITextField and start over with a new one.
     
  8. Parker21 thread starter macrumors newbie

    Joined:
    Sep 19, 2014
    #8
    I have tried creating a new field with no luck I even created a new view controller and set it as the root. The new view controller didn't have a class so there was no code attached to it. It's really strange the "text field" aligns correctly in xCode its in the simulator when it forces left.

    However if it's blank the placeholder will respect the align wether it be left right or centre. As soon as you enter anything and you click another text field or close the keyboard (lose focus). It forces left?

    If you set the field as *secure* so password field. That actually aligns correctly. Posting the code is a difficultly as its for a clien. I don't normally ask for help but I thought another programmer might see something I'm missing.

    Please help!
     
  9. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    What version of Xcode are you using?

    Here's what I did.

    Create a new project, choose Single View Application.
    To the Main.storyboard I added two UITextFields and adjusted their width to 170.
    I set the Text Field/Alignment to Center
    I added some text to each text field 'Hello' and 'There'
    I set the Placeholder text for each text field to 'Placeholder'
    I then ran the app in one of the iPad Simulators

    The text and placeholder text is always centered. If I close the keyboard the text remains centered.

    What are you doing differently than that?
     
  10. Parker21 thread starter macrumors newbie

    Joined:
    Sep 19, 2014
    #10
    That works.

    That works if I create a new document and do it, so it means theres something in my app stopping it. I have checked app delegate, but nothing suppressing my fields or controlling them. I am using cocca pods as I have BrainTree payments in there, but this issue was from before then. I have fixed everything else apart from this, but not it needs fixing, its not an option to "re-create" the entire project. Theres too much to transfer.

    Just wondering if theres any code which can "force" this to happen so I can search for it see if it still occurs.
     
  11. Parker21 thread starter macrumors newbie

    Joined:
    Sep 19, 2014

Share This Page