【问题标题】:Last Button in UIScrollView add target doesn't effectUIScrollView 添加目标中的最后一个按钮不起作用
【发布时间】:2017-08-05 15:34:19
【问题描述】:

我有一个类别列表添加我为滚动视图中的每个类别创建的按钮 最后一个按钮是所有产品的 ALL 按钮。但是有些我没有添加任何操作。 这是我的代码 首先,我创建一个函数来创建添加到视图的按钮

func catButtonView(buttonSize:CGSize) -> UIView {
    let buttonView = UIView()
    buttonView.backgroundColor = UIColor(colorLiteralRed: 1, green: 1, blue: 1, alpha: 0)
    buttonView.frame.origin = CGPoint(x: 0,y: 0)
    let padding = CGSize(width:0, height:0)
    buttonView.frame.size.width = (buttonSize.width + padding.width) * CGFloat(categories.count)
    buttonView.frame.size.height = (buttonSize.height +  2.0 * padding.height )
    //add buttons to the view
    var buttonPosition = CGPoint(x:padding.width * 0.5, y:padding.height)
    let buttonIncrement = buttonSize.width + padding.width
    let hueIncrement = 1.0 / CGFloat(categories.count)
    var newHue = hueIncrement
    for i in 0...(categories.count)  {

        let button = UIButton.init(type: .custom) as UIButton
        button.frame.size = buttonSize
        button.frame.origin = buttonPosition
        button.setBackgroundImage(R.image.button(), for: .normal)
        button.setBackgroundImage(R.image.button_selected(), for: .selected)

        if(i==categories.count) {
            button.setTitle("ALL", for: .normal)
        } else {
            button.setTitle(categories[i].name, for: .normal)
        }
        button.setTitleColor(UIColor.white, for: .normal)
        buttonPosition.x = buttonPosition.x + buttonIncrement
        newHue = newHue + hueIncrement
        button.tag = i+1
        button.addTarget(self, action: #selector(catButtonPressed(sender:)), for: .touchUpInside)
        buttonView.addSubview(button)


    }
    return buttonView
}

这是动作函数

func catButtonPressed(sender:UIButton){
    print(sender.tag)
    if(self.selectedCat != sender.tag-1) {
        let lastBtn = catScrollView.viewWithTag(selectedCat+1) as? UIButton
        lastBtn?.setBackgroundImage(R.image.button(), for: .normal)
        self.selectedCat = sender.tag-1
        self.itemsCollection.reloadData()
        sender.setBackgroundImage(R.image.button_selected(), for: .normal)
    }
}

然后添加到滚动视图

let catScrollingView = catButtonView(buttonSize: CGSize(width: 200.0, height:50.0))
    catScrollView.contentSize = catScrollingView.frame.size
    catScrollView.subviews.forEach({ $0.removeFromSuperview() })
    catScrollView.addSubview(catScrollingView)
    catScrollView.showsHorizontalScrollIndicator = true
    catScrollView.indicatorStyle = .default

    self.itemsCollection.reloadData()

有人知道我错过了什么吗?

【问题讨论】:

  • 检查您的滚动视图高度和按钮位置并更改一行 button.addTarget(self, action: #selector(catButtonPressed(sender:)), for: .touchDown) 并检查输出跨度>
  • 谢谢,我想我知道问题所在,框架宽度仅适用于 categories.count,而不是 categories.count +1,其中“ALL”按钮是
  • 是的,我想告诉你,你的按钮不在视图中,所以将它添加到视图中

标签: ios uiscrollview swift3 uibutton


【解决方案1】:

这是因为你的catButtonPressed函数中的if statement加上最后一个按钮的标签是假的,导致里面的代码永远不会被执行。

假设您有 5 个类别,因此您的 categories.count = 5。所以你的self.selectedCat 的范围是从 0 到 4。在您的for i in 0...(categories.count) 中,最后一个按钮的标签是button.tag = 5

现在在您的 catButtonPressed 函数中。如果最后一个按钮是最后一个类别,那么您的 self.selectedCat 将是 4sender.tag 将是 5

func catButtonPressed(sender:UIButton){
    print(sender.tag)
    if(4 != 5-1) { 
         // Any code in here will never be executed because 4 != 4 is false
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    相关资源
    最近更新 更多