Ios UIScrollView按钮点击不工作
我在水平UIScroll视图中排列了UIButtons。我有添加到UIScrollView的contentView UIView,按钮就在里面。scrollview内容宽度大于屏幕大小,因此一些按钮不在屏幕上。当我滚动到屏幕上并将它们拖到屏幕上时,点击它们并不会调用它应该使用的功能。另一方面,点击最初在屏幕上启动的按钮可以工作。如果您能帮助解决此问题,我们将不胜感激。以下是一个小的工作示例,以防有所帮助: 类ViewController:UIViewController{Ios UIScrollView按钮点击不工作,ios,swift,uiscrollview,uibutton,Ios,Swift,Uiscrollview,Uibutton,我在水平UIScroll视图中排列了UIButtons。我有添加到UIScrollView的contentView UIView,按钮就在里面。scrollview内容宽度大于屏幕大小,因此一些按钮不在屏幕上。当我滚动到屏幕上并将它们拖到屏幕上时,点击它们并不会调用它应该使用的功能。另一方面,点击最初在屏幕上启动的按钮可以工作。如果您能帮助解决此问题,我们将不胜感激。以下是一个小的工作示例,以防有所帮助: 类ViewController:UIViewController{ @IBOutl
@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")
}
}
您是否仅将
UIScrollView
用于按钮?是否还有其他内容?如果仅用于UIButtons
,我将使用UICollectionView
而不是滚动视图。我认为您的按钮不在内容视图中,请检查此项,在内容视图中添加任何颜色,并在scroll中添加不同的颜色l查看,然后查看按钮是否在视图中