The best way to center buttons/labels etc...

Discussion in 'iOS Programming' started by MythicFrost, Dec 20, 2009.

  1. MythicFrost macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #1
    OK, in Visual Basic I'd simply add the buttons labels and what not to a Picture Box or some form of group object, that way I center the picture box in the middle of the window, and the content is centered because it's in the (invisible) picture box.

    Is there some kind of thing like that that I can use?
    Currently I'm doing:
    Code:
    [bButton setCenter:(CGPointMake(iWidth/2, (iHeight/2)))];
    But if I have multiple objects I have to calculate the distance and height of each object and adjust the Y position of each one to get them to be exactly centered, I'd like some kind of control I can add them to.

    Any idea's? Thanks.
     
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  3. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #3
    Alright I've done that, but it is for dealing with the orientation of the device right?
    All I want to do is find an easy way to quickly position controls, rather than calculating it all in my head.

    I've set
    Code:
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleHeight;
    What would I do to force the orientation of the device to sideways? that's the way my app is intended to be held anyway.

    Thanks
     
  4. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    The autoresizingMask deals with positioning a view relative to its superview. When the orientation changes the view will be re-positioned automatically based on the autoresizingMask.

    However, if you're asking: "can I just throw a view out there and get it to be automatically positioned" I think the answer is no. It needs an initial position.

    Of course if you lay your UI out in IB then this isn't really a problem.
     
  5. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #5
    I think I'm doing ok doing it via maths, but I'm interested in getting my view to change when it rotates. Just out of curiosity.

    I've got my button in the very center of the screen, when I flip the device to the left though, it doesn't rotate around it's center. Do you know how I could get that result?

    Thanks
     
  6. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    Depends on exactly what you mean by flip around its center.

    Using an autoresizingMask of flexible width and height the view will be adjusted when its parent is resized due to rotation. You need all the containing views in the view hierarchy to do this. Typically there is one container view that is the view controller's view property and then you add your controls to that view. Make sure that the container view has its autoresizingMask set.
     
  7. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #7
    In MyGameViewController.m in viewDidLoad() I create all my controls, and I add them to the view by [[self view] addSubview:()];

    When I rotate the device, nothing happens.
     
  8. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Did you implement shouldAutorotateToInterfaceOrientation: and return YES?

    If you did that already what do you mean by nothing happens? Does anything appear rotated?
     
  9. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #9
    No I didn't implement that, how do I do that?
    EDIT: I've done this:
    Code:
    [self shouldAutorotateToInterfaceOrientation:(YES)];
    
    Nothing rotates, it looks correct if the device has the home button at the bottom, but if I turn it to the left it looks wrong, no rotating.
    EDIT: Image attached.
     

    Attached Files:

  10. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    That's not it.

    In your view controller add this code:

    Code:
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations
    	return YES;
    }
    
    You do have a view controller don't you?

    Subclasses of UIViewController must or may implement various methods that are called by the system. This particular one reports to the system what orientations the view controller supports.
     
  11. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #11
    Alright thanks, it's working.

    How can I force the view to stay horizontally?
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  13. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    Normally when you create a project from a template it creates a view controller and it has this method in it. The stock version of this method works only in portrait orientation. Like this:

    Code:
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations
    	return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    You should be able to make that work with landscape instead.

    FWIW, apps that only work in landscape are a bit of a pain to set up. Is that your goal?
     

Share This Page