我有一个可以使用 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
最佳答案
你只需要添加按钮到 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)
关于ios - 如何在 Swift 中让按钮随背景滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30794497/