Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击iOS滚动视图中的按钮添加边框,并从其他按钮中删除边框_Ios_Uiscrollview_Uibutton_Swift4 - Fatal编程技术网

单击iOS滚动视图中的按钮添加边框,并从其他按钮中删除边框

单击iOS滚动视图中的按钮添加边框,并从其他按钮中删除边框,ios,uiscrollview,uibutton,swift4,Ios,Uiscrollview,Uibutton,Swift4,我在iOS中为水平滚动视图添加了按钮 override func viewDidLoad() { super.viewDidLoad() setUpScrollView() // Do any additional setup after loading the view, typically from a nib. } func setUpScrollView() { let buttonPadding:CGF

我在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的边框变为白色

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

试试这段代码

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
}

我认为问题在于您的按钮只能在
setScrollView
中访问。 因此,当点击按钮时,在@Anton answer中,只有点击的按钮在
didTap
功能中是已知的。

我认为更好的办法是制作一个
ui按钮数组
, 在
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
}


}

我想你希望当一个按钮点击时,其他按钮变为白色,对吗?假设我有10个btn,我希望当点击btn 1时,btn的边框变为白色,其他的保持黑色,如果点击btn 2,则所有按钮再次变为黑色,包括btn1,只是btn 2的边框变为白色。但是,您提供的代码使btn1的边框保持白色(已单击),以防btn 2单击。这很有效。谢谢,我们只想再添加一个ting,以便如果有人来这里寻求任何解决方案,可以一次性获得整个解决方案……只需将滚动视图中的按钮附加到按钮上。