gpt4 book ai didi

ios - UIScrollView 按钮点击不起作用

转载 作者:行者123 更新时间:2023-11-30 13:49:30 24 4
gpt4 key购买 nike

我将 UIButtons 排列在水平 UIScrollView 中。我已将 contentView UIView 添加到 UIScrollView 中,按钮位于其中。 ScrollView 内容宽度比屏幕大小更宽,因此某些按钮一开始就不在屏幕上。当我滚动并将它们拖到屏幕上时,点击它们不会调用它应该的功能。另一方面,点击最初在屏幕上启动的按钮是有效的。希望能帮助您解决问题。

最佳答案

这是一个小例子,如果有帮助的话:

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")
}
}

关于ios - UIScrollView 按钮点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34467924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com