0

I created a button to add an UIImageView as a subview of a UIView(named backgroundView), and then I want all the UIImageViews can be drag within the UIView(but not out of it), so I use "backgroundView.cliptobounds = true" but it doesn't work.

So how can I add a subview via button while after adding the subview, I can drag the subviews around within the bounds of the parent UIView?

class ViewController: UIViewController {

@IBOutlet weak var addSubview: UIButton!

@IBOutlet weak var backgroundView: UIView!


override func viewDidLoad() {
    super.viewDidLoad()
    backgroundView.clipsToBounds = true

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func buttonAddSubview(sender: AnyObject) {
    let labelText = sender.titleLabel!?.text!
    var subview : UIImageView
    subview  = UIImageView(frame:CGRectMake(300, 300, 200, 200));
    subview = UIImageView(image: UIImage(named: labelText!))


    backgroundView.addSubview(subview)
    backgroundView.clipsToBounds = true

    subview.image = UIImage(named:labelText!)
    }

}

Szewing
  • 27
  • 1
  • 3

0 Answers0