Auto Layout Problem

Discussion in 'iOS Programming' started by erdinc27, Jul 25, 2013.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #1
    hey all. i want to use Auto Layout for the first time in my project. but i am facing with a problem. i have 6 buttons next to each other and i want to align them horizontally centered in both landscape and portrait orientations. it is done in portrait but i have problem in landscape mode. here a screenshot how it looks in both modes. so how i can do same thing in landscape mode also ?

    landscape mode : http://img69.imageshack.us/img69/8995/g6q.png

    portrait mode : http://img560.imageshack.us/img560/4865/qu2u.png

    so how i can use Auto Layout for that purpose ?
     
  2. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #2
    How have you setup the constraints for each button?

    Edit: Wait - I seem to have miss understood you.

    You're asking how to do it and not why it's not working?

    Have a read here on auto layout:

    http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

    Read both parts and you will be able to solve this issue easily in your app.
     
  3. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #3
    thanks for the reply. i saw that article before. i tried to apply the examples there but it didnt work. i will try it again.
     
  4. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #4
    Patience and persistence is key to development I have found.

    If at first you don't succeed, try, try again.
     
  5. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #5
    thanks for the help. i found such a solution.
    ViewController.h
    Code:
    @property (retain, nonatomic) IBOutlet NSLayoutConstraint *leadLayout;
    
    ViewController.m
    Code:
    - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
            self.leadLayout.constant = 124.0 ;
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            self.leadLayout.constant = 52.0 ;
            
    }
    
    and between buttons i put Horizontal Spacing Constraint. now it works good.
    i make the examples run in the link u sent to me.
     
  6. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #6
    I know you got it working, but why not just align the two middle ones (with an offset from the horizontal center) and then put constraints on the horizontal spacing for the rest of them
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Yeah, you should be able to achieve this without any constraints modification in code.
     
  8. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #8
    actually i thought it in theory but i couldnt make it work in xcode. although the code that i wrote down is working i am still work on the idea that u told. i want to make it work also. i appreciate if u can show how to do it
     

Share This Page