1

I use swift 2.0 for programming and I'm confused with adding more objects on view controller. For example, I need 14 buttons on my view controller with required constraints. But I'm able to add only 7 buttons one by one in my view controller on story board. Because no more place to add objects on view controller. So, How to add number of objects in view controller on story board?. Please refer following screen shots. Thanks for your help friends.

View controller on story board:

enter image description here

I'm able to add only 7 buttons with proper constraints. but i need to add 14 buttons with proper constraints. when view controller is scrolled then the button 8 to 14 will shown one by one respectively button 1 to 7.

Output on iPhone 5 without scroll view :

enter image description here

Output on iPhone 4s with scroll view:

enter image description here I need like this on iPhone 5 , but 14 buttons. The 8 to 14 buttons arranged respectively 1 to 7 buttons.

Rajamohan S
  • 7,229
  • 5
  • 36
  • 54

3 Answers3

3

You can lay this out in a Storyboard.

  1. Click on your viewController, and then in the Size Inspector, set the Simulated Size to Freeform and set the width to 320 and the height to 1136. This will give you a tall skinny layout.

    Freeform layout

  2. Add a scrollView to that. Size the scrollView to the full screen. Pin all four of its sides to its superView.

  3. Add a UIView to the scrollView. This is your contentView. Size the content view to the full screen. Pin all four of its sides to the scrollView.
  4. Set an Equal Widths constraint between the contentView and the scrollView. This will only allow it to scroll vertically.
  5. Set a height constraint of 1136 for the contentView.
  6. Lay out your 14 buttons in the tall viewController.

    Xcode showing the tall viewController with 14 buttons

When you run, your contentView will scroll.


demo in iPhone 5 simulator

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • This is the way I would do it. – Duncan C Sep 03 '16 at 00:33
  • Wow superb. This is exactly i need :D . thank you Mr.Vacawama – Rajamohan S Sep 03 '16 at 00:41
  • @vacawama When i am freeform viewcontroller more than 4000 it's showing black entire screen .what's the problem? – Vadlapalli Masthan Jul 11 '20 at 06:39
  • I just made one with a height of 5000. Be careful to follow the instructions exactly. Make sure your constraints are set to the Superview for both the scrollview and its content view. I expanded my height to 7000 after 5000 was successful. I had to change both the simulated height and the height constraint for the view. Then when I zoomed in and out in Interface Builder the view was rendered correctly. – vacawama Jul 11 '20 at 11:37
1

You can add button so easily by using some codes instead of Storyboard.

    let scrView = UIScrollView(frame: CGRect(x: 0, y: 0, width: SCR_W, height: SCR_H))
    for idx in 1...14 {
        let button = UIButton(frame: CGRectMake(0, 0, btnWidth, btnHeight))
        button.center = CGPointMake(SCR_W/2, offset * idx)
        button.setTitle("Button\(idx)", forState: .Normal)
        button.tag = idx
        button.addTarget(self, action: #selector(self.onClick(_:)), forControlEvents: .TouchUpInside)
        scrView.addSubview(button)
    }

    scrView.contentSize = CGSizeMake(SCR_W, offset * 14)
    self.view.addSubView(scrView)

    -------->

    func onClick(let btn:UIButton) -> Void {
    switch btn.tag {
       case 1:
       case 2:
       case 3: 
     ...  ... ...
       default:

    }
}
AppHero2
  • 681
  • 7
  • 25
  • ouch sorry brother. i think I'm not ask clearly. I need to add 14 buttons using storyboard. Each button will have individual action. but I'm able to add 7 buttons. Is any method possible for make this with storyboard only? – Rajamohan S Sep 03 '16 at 00:01
  • no worries, I have upadeted my answers – AppHero2 Sep 03 '16 at 00:06
  • mmmm superb brother. But I'm try to achieve this by story board only :) because if i use lot of objects(button,labels,text field...etc) then coding method will make me confuse. Thanks for your help brother! – Rajamohan S Sep 03 '16 at 00:15
0

You can change your viewController size in Storyboard.

select viewController and change size to Freedom (fig1.)

enter image description here

   fig1.

select view of viewController and change frame. (fig2.)

enter image description here

   fig2.
AppHero2
  • 681
  • 7
  • 25