0

I have a storyboard in which there is a separate view controller for a UIView. In that UIView I have to load 10 buttons in a single row in the case of IPad else I have to load 10 buttons in two rows. I have tried using 2 separate xib files and loading them statically but I want to do it programatically using view controllers.Storyboard Image I have added the screenshot of my storyboard.

Can anybody guide me on changing the views dynamically.Here is the code of the new ViewController for the buttons.

import Foundation

 class TNPSRatingsViewController: UIViewController {

    var selectedScoreButton: TNPSScoreButton?
    var scoreSelectedOnce = false

    @IBOutlet var buttons: [TNPSScoreButton]!


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.modifyPositionOfTNPSButton()
    }

    override func viewWillTransition(to size: CGSize,
                                     with coordinator: UIViewControllerTransitionCoordinator) {
        coordinator.animate(alongsideTransition: nil, completion: ({ [unowned self] _ in
            self.modifyPositionOfTNPSButton()

        }))
    }


    // MARK: - Private functions
    fileprivate func modifyPositionOfTNPSButton() {
        if UIApplication.shared.delegate?.window??.traitCollection.horizontalSizeClass == .compact {
            //
            //
        } else {
            //
            //

        }
    }



}

Thanks in Advance

Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34
  • you want to ad button programmatically ? – iOS Geek Mar 14 '18 at 04:23
  • @iOSGeek yeah I have edited the question with the viewcontroller code in which the checks what kind of device is and I dont know how to proceed with the code to create those buttons programatically for two different views – Vishnu M Menon Mar 14 '18 at 04:30
  • you just want to load different subview in container View depending on iPhone or iPad right ? – iOS Geek Mar 14 '18 at 04:32
  • yeah tats it. single row of buttons on iPad and double row of buttons on iPhone – Vishnu M Menon Mar 14 '18 at 04:34
  • Please check my answer , as you had already created two different stackViews just you want to replace the Subview in Container view depending upon iPad or iPhone , you can try my answer , simply it will work – iOS Geek Mar 14 '18 at 04:38
  • @iOSGeek I will try and let you know once I am done – Vishnu M Menon Mar 14 '18 at 05:25

2 Answers2

0

How about using a UICollectionView?

on iPad, use 1 row of 10 items on iPhone use 2 rows of 5 items

You can stick to view controller containment, or implement the uicollectionview directly with your first screen being the delegate/datasource.

Mitchell Currie
  • 2,769
  • 3
  • 20
  • 26
0

Try making a class that will tell that current device is iPad or iPhone

class Env
{
    //Check if its iPad
    static var iPad: Bool
    {
        return UIDevice.current.userInterfaceIdiom == .pad
    }
}

Now While you are require to add subview in containerView

just make use of that class as:

class ViewController: UIViewController {

    var myCustomContainerView = UIView()
    var subviewCreatedXIBForiPad = UIView()
    var subviewCreatedXIBForiPhone = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        Env.iPad ? myCustomContainerView.addSubview(subviewCreatedXIBForiPad) : myCustomContainerView.addSubview(subviewCreatedXIBForiPhone)
    }
}
iOS Geek
  • 4,825
  • 1
  • 9
  • 30
  • In the case of split view mode in Ipad it will only load the Xib for ipad ri8? I want to load the view of iPhone in the case of splitview – Vishnu M Menon Mar 14 '18 at 04:44
  • This is working as env.ipad ? yes iPad (add views for iPad) : its iPhone (add Views for iPhone) – iOS Geek Mar 14 '18 at 04:46
  • Just use this check wherever you want to load Views depending on iPhone or iPad – iOS Geek Mar 14 '18 at 04:47
  • What i have tried earlier is that i had 2 xib file say tnps.xib and tnps~ipad. it will automatically loads the xib file according to the device. And In the case of Ipad SplitView, the xib file loaded was of Ipad. I want to load xib file for Iphone in this case. so env.ipad always return true in the case of ipad or will it return false in the case of split view – Vishnu M Menon Mar 14 '18 at 04:55