【问题标题】:Add border to buttons in iOS scroll view on click and remove border from other buttons单击时为iOS滚动视图中的按钮添加边框并从其他按钮中删除边框
【发布时间】:2018-10-02 11:37:32
【问题描述】:

我在 iOS 的水平滚动视图中添加了按钮。

override func viewDidLoad() {
        super.viewDidLoad()
        setUpScrollView()
        // Do any additional setup after loading the view, typically from a nib.
    }

     func setUpScrollView() {
        let buttonPadding:CGFloat = 10
        var xOffset:CGFloat = 10

        for i in 0 ... 10 {
            let button = UIButton()
            button.tag = i
            button.backgroundColor = UIColor.red
            button.setTitle("\(i)", for: .normal)

            if(button.tag==currentTag){
           button.addTarget(self, action: #selector(btnTouchUnselect), for: UIControlEvents.touchUpInside)
            }
            else{

                     button.addTarget(self, action: #selector(btnTouch), for: UIControlEvents.touchUpInside)
            }
            button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 70, height: 30)

            xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width;
            scrollView.addSubview(button)
        }

        scrollView.contentSize = CGSize(width: xOffset, height: scrollView.frame.height)
    }

    @objc func btnTouch(button:UIButton){
        print("tap touch",button.tag)
            button.layer.borderColor = UIColor.black.cgColor
            button.layer.borderWidth = 1.0
        currentTag = button.tag

    }

    @objc func  btnTouchUnselect(button:UIButton){
        button.layer.borderColor = UIColor.white.cgColor
        button.layer.borderWidth = 1.0

    }

}

我希望一个按钮在用户单击它时获得不同的边框颜色,而其他按钮保持黑色。但是,当我使用此代码时,它会将所有单击的按钮边框变为黑色,并且不会将单击的按钮变为白色。

目标示例:-假设我有 10 个按钮,我希望在单击按钮 1 时,其边框变为白色,其他按钮保持黑色;如果单击按钮 2,则包括按钮 1 在内的所有按钮的边框都会再次变为黑色,只有按钮 2 的边框会变为白色。

我需要一些指导来实现这一目标。

【问题讨论】:

  • 我想你想当一个按钮被点击时其余的按钮变成白色,对吗?

标签: ios uiscrollview uibutton swift4


【解决方案1】:

试试这个代码

var allButtons = [UIButton]() 

override func viewDidLoad() {
    super.viewDidLoad()
    setUpScrollView()
    // Do any additional setup after loading the view, typically from a nib.
}

 func setUpScrollView() {
    let buttonPadding:CGFloat = 10
    var xOffset:CGFloat = 10

    for i in 0 ... 10 {
        let button = UIButton()
        button.tag = i
        button.backgroundColor = UIColor.red
        button.layer.borderWidth = 1.0
        button.setTitle("\(i)", for: .normal)
        button.addTarget(self, action: #selector(didTap), for: UIControlEvents.touchUpInside)
        button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 70, height: 30)

        xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width;
        scrollView.addSubview(button)
        allButtons.append(button)
    }

    scrollView.contentSize = CGSize(width: xOffset, height: scrollView.frame.height)
}

@objc func didTap(button: UIButton) {
    print("tap touch",button.tag)

    allButtons.forEach { inButton in 
         if inButton == button {
            button.layer.borderColor = UIColor.black.cgColor
         } else {
            button.layer.borderColor = UIColor.white.cgColor
         }
    }
    currentTag = button.tag
}

【讨论】:

  • 假设我有 10 个 btn,我希望当 btn 1 被点击时,btn 的边框变成白色,其他的保持黑色,如果 btn 2 被点击,那么所有的东西都会再次变成黑色,包括 btn1 只是 btn 2 的边框发生变化到白色。但是,您提供的代码在 btn 2 点击的情况下保持 btn1 的边框也为白色(已被点击)。
【解决方案2】:

我认为问题在于您的按钮只能在 setScrollView 中访问。 所以当一个按钮被点击时,在@Anton 的回答中,只有被点击的按钮在didTap 函数中是已知的。

我认为一个更好的主意是创建一个UIButtons 的数组, 在setScrollView 发起他们, 然后使用@Anton didTap 函数

class yourClass {

var buttons : [UIButton] = Array(repeatElement(UIButton(), count: 10))

override func viewDidLoad() {
        super.viewDidLoad()
        setUpScrollView()
        // Do any additional setup after loading the view, typically from a nib.
    }

func setUpScrollView() {
        let buttonPadding:CGFloat = 10
        var xOffset:CGFloat = 10

        for i in 0...9 {
            buttons[i].tag = i
            buttons[i].backgroundColor = UIColor.red
            buttons[i].setTitle("\(i)", for: .normal)

         //Other functionality that you had set here before...
    }

@objc func didTap(clickedButton: UIButton) {

    for eachButton in self.buttons { 
         if eachButton.tag == clickedButton.tag {
            eachButton.layer.borderColor = UIColor.white.cgColor
         } else {
            eachButton.layer.borderColor = UIColor.balck.cgColor
         }
    }
    currentTag = clickedButton.tag
}


}

【讨论】:

  • 这行得通。谢谢,如果有人来这里寻求任何解决方案,就可以一次性获得整个解决方案......只需在滚动视图中附加按钮到按钮。
猜你喜欢
  • 2022-12-04
  • 2021-09-24
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 2020-03-27
  • 2015-12-16
  • 1970-01-01
相关资源
最近更新 更多