How to have buttons scroll with background in Swift?

1.5k views Asked by At

I have an image that you can scroll through using UIScrollView. I want to add buttons to it. The buttons need to stay attached to the image and scroll with it, not separately. I need this all done programmatically.

This is the code I have right now:

imageView = UIImageView(image: UIImage(named: "Map 1.png"))

// 2
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
// 3
scrollView.contentSize = imageView.bounds.size
// 4
scrollView.addSubview(imageView)
view.addSubview(scrollView)

scrollView.contentSize.height = scrollView.frame.size.height
1

There are 1 answers

1
vinhvx On BEST ANSWER

you just need to add button to scrollview:

var button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Button", forState: UIControlState.Normal)
button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
scrollView.addSubview(button)