2

I’m still trying to get my head around constraints and the objective c best practises in general.

I need to dynamically create between 1 to 6 UIButtons depending on a variable. I’m ok with this part but confused with how to position them given that:

  1. The number of buttons could be anything from 1-6.
  2. The buttons have to be of equal size.
  3. Landscape and Portrait orientations will have different layouts.
  4. They have to ‘float’ left in the landscape view and vertically align to the top in portrait view.

enter image description here

I'm guessing I'll need one container that sits at the bottom of the view and changes height depending on the orientation, then have rules for the buttons inside depending on the container width. But how to go about it I have no idea.

Dol
  • 944
  • 3
  • 10
  • 25

1 Answers1

1

In the long run it will be better to use the UICollectionView. Subclass UICollectionView and add it in your Storyboard. Make an outlet of the collection view to your view controller and set its delegate and data source. Add the <UICollectionViewDelegate> and <UICollectionViewDataSource> to your view controller and implement the delegate methods. For the buttons, make a custom UICollectionViewCell with a UIButton as its subview. Then in cell:ForRowAtIndexpath: method, initialize the UICollectionViewCell subclass, and set the properties of the UIButton. This might not be the answer you wanted, but UICollectionView offeres much more flexibility as far as layouts are concerned. You can read more about UICollectionViewDelegateFlowLayout here.

prithvi.a
  • 26
  • 2
  • Thanks for the info! I'm going to research it all now and get back to you :) – Dol Jan 06 '14 at 19:01
  • Yes this is the way forward, found a great tutorial here -> http://www.appcoda.com/ios-programming-uicollectionview-tutorial/ – Dol Jan 06 '14 at 19:24