Frank Boccia Frank Boccia - 5 months ago 20
iOS Question

Creating multiple UIViews from button pressed Swift

I have a UIView called brokeView, It is draggable and rotational. I am trying to allow the user to populate the same exact view from clicking a button allowing an endless amount of the same UIView to populate. Is this possible to do in swift using the storyboard?
- Do I have to create all the views in storyboard or could I populate them through code using an array or loop?

@IBOutlet weak var brokeView: UIView!

@IBAction func showBrokenAnnotation(sender: AnyObject) {
brokeView.hidden = false
for i in 1...10 {
print(i)
let newView = UIView(frame: brokeView.bounds)
brokeView.addSubview(newView)
}

Answer

This can be done through code. Here is some pseudo code.

Updated from our conversation below - this makes it the same size as your "brokenView" and then you can set the x/y however you want.

 let newView = UIView(frame: CGRect(x: 0, y: 0, width: mainView.frame.width, height: mainView.frame.height))

 self.view.addSubview(newView)
Comments