如何让按钮在Swift中滚动背景?

时间:2015-06-12 02:34:29

标签: ios xcode swift uiscrollview scrollview

我有一个可以使用UIScrollView滚动浏览的图像。我想为它添加按钮。按钮需要保持与图像的连接并随之滚动,而不是单独滚动。我需要以编程方式完成这一切。

这是我现在的代码:

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 个答案:

答案 0 :(得分:0)

你只需要向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)