我将UIButtons排列在水平UIScrollView中。我有我已添加到UIScrollView的contentView UIView,按钮在里面。 scrollview内容宽度比屏幕大小宽,因此一些按钮开始时不在屏幕上。当我滚动并将它们拖到屏幕上时,点击它们并不会调用它所应用的功能。另一方面,点击最初在屏幕上启动的按钮可以正常工作。在解决这个问题时会感谢一些帮助。
答案 0 :(得分:0)
这是一个小小的工作示例,如果它有帮助:
类ViewController:UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let button1 = UIButton()
button1.backgroundColor = UIColor.redColor()
button1.setTitle("Button 1", forState: .Normal)
button1.setTranslatesAutoresizingMaskIntoConstraints(false)
self.scrollView.addSubview(button1)
self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(100)-[button1(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["button1": button1]))
self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[button1(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["button1": button1]))
button1.addTarget(self, action: "button1", forControlEvents: .TouchUpInside)
let button2 = UIButton()
button2.backgroundColor = UIColor.greenColor()
button2.setTitle("Button 2", forState: .Normal)
button2.setTranslatesAutoresizingMaskIntoConstraints(false)
self.scrollView.addSubview(button2)
self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(500)-[button2(100)]-(300)-|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["button2": button2]))
self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[button2(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["button2": button2]))
button2.addTarget(self, action: "button2", forControlEvents: .TouchUpInside)
}
func button1() {
print("button1")
}
func button2() {
print("button2")
}
}