【问题标题】:How to have buttons scroll with background in Swift?如何让按钮在 Swift 中随背景滚动?
【发布时间】:2015-06-12 02:34:29
【问题描述】:

我有一张可以使用 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

【问题讨论】:

  • 您需要将图像视图和按钮嵌入到 UIView 中,然后将该视图添加为滚动视图中的内容

标签: ios xcode swift uiscrollview scrollview


【解决方案1】:

你只需要在滚动视图中添加按钮:

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)

【讨论】:

  • 这种方式会导致难以设置滚动视图的内容大小
猜你喜欢
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多