I have a UIScrollView set up, and am using addSubView to add 5 buttons to it. Here is the code inside my viewDidLoad.
let codedButton:UIButton = UIButton(frame: CGRectMake(0, 0, 100, 50))
codedButton.backgroundColor = UIColor.redColor()
codedButton.setTitle("Button 1", forState: UIControlState.Normal)
codedButton.addTarget(self, action: #selector(ViewController.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
codedButton.tag = 1
self.buttonScrollView.addSubview(codedButton)
let codedButton2:UIButton = UIButton(frame: CGRectMake(110, 0, 100, 50))
codedButton2.backgroundColor = UIColor.redColor()
codedButton2.setTitle("Button 2", forState: UIControlState.Normal)
codedButton2.addTarget(self, action: #selector(ViewController.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
codedButton2.tag = 2
self.buttonScrollView.addSubview(codedButton2)
(all the way to 5.)
The problem that I need help with is... Ultimately, these buttons will be dynamically displayed. Not just a set amount of 5.
How do I adjust the X value of the CGRectMake to automatically "float" new buttons to the right of the button that was just created? In my code above, I am explicitly stating where the X is, but that won't work properly once these buttons become dynamically created in code.
EDIT To clarify my question above: I have attached an image. Note how the buttons always stack horizontally / next to each other, regardless of if there's 2 3 4 or 5. These buttons will be dynamically shown. Therefore, with my code attempt I have provided above, it will not work because it is explicitly setting where to place the buttons on the X axis.