0

I need some help in implementing this UI using Autolayout

I am creating UIButtons programatically

    for (int i=0; i<10; i++) {
       UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
       btn.backgroundColor = [UIColor greenColor];
       [btn sizeToFit];
       [btn setTitle:@"user@example.com" forState:UIControlStateNormal];
       [self.myCustomView addSubview:btn];

    }

myCustomView is a UIView created in XIB . I have pinned myCustomView's leading,trailing & top edges to its superview . I have set the height of myCustomView to 44.

I need to add UIButtons as subview to myCustomView using constraints in such a way that the buttons should wrap to next line and myCustomView should increase its height based on its subview content.

http://i41.tinypic.com/3536ob6.png

Snippet of code to achieve this will be helpful

Bharath
  • 51
  • 5
  • you to have to specify the frame size if you are adding buttons programatically... – BalaChandra Oct 15 '13 at 12:41
  • Are you sure about adding frame size? Auto layouts is meant to handle the frame by itself right ? – Bharath Oct 15 '13 at 13:58
  • http://stackoverflow.com/questions/12593153/implementing-auto-layout-for-views-generated-programmatically visit the link may be not about buttons but some info about auto layout – BalaChandra Oct 16 '13 at 04:00

1 Answers1

1

If you want to use auto layout programmatically you can implement it by using NSLayoutConstraint or a library Parus https://github.com/DAlOG/Parus

BalaChandra
  • 632
  • 9
  • 33
  • Thanks for this github link but still it doesn't solve my problem. I did apply constraints but all the buttons are coming in a single line & few are not visible (I think my constraints are incorrect). I am trying to create buttons programatically and I want to place it in a view which should increase its height dynamically, something similar to To Address in ios mail app (using AutoLayouts) – Bharath Oct 16 '13 at 04:31
  • I never used autolayout i also dint know much about layout constraints, post them somebody may help you with them.... – BalaChandra Oct 16 '13 at 04:33
  • If you are going to use autolayout then why are you not using xib files or storyboard – BalaChandra Oct 16 '13 at 04:34
  • I am using storyboard. If you look at the link that i have posted in my question, the red & blue colored box are UIView created on storyboard. I have pinned red View to leading,trailing & top edge of super view. I have fixed the height of red View to 44 & changed the priority to 999. Now i am creating UIbuttons programatically and adding it as subView to red View. Its coming in single line as shown in the image . I want them to wrap when it exceed red view width – Bharath Oct 16 '13 at 04:38