【问题标题】:UIScrollView Button Tap Not WorkingUIScrollView 按钮点击不起作用
【发布时间】:2015-12-26 02:03:42
【问题描述】:

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

【问题讨论】:

  • 您是否仅将UIScrollView 用于按钮?还有其他内容吗?如果它只用于UIButtons,我会使用UICollectionView 而不是滚动视图。
  • 我认为您的按钮不在您的内容视图中,请检查此项为您的内容视图添加任何颜色并为您的滚动视图添加不同的颜色,然后查看您的按钮是否在视图中

标签: ios swift uiscrollview uibutton


【解决方案1】:

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

类视图控制器: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")
    }
}

【讨论】:

    猜你喜欢
    • 2013-09-10
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2015-10-25
    • 1970-01-01
    相关资源
    最近更新 更多