【问题标题】:Swift remove a button BottomBorderSwift 移除一个按钮 BottomBorder
【发布时间】:2017-11-12 19:30:28
【问题描述】:

所以我的按钮在单击时会添加漂亮的边框,但我找不到删除边框的方法,因为我一次只想要一个边框。我尝试将宽度设置为 0 等。

@IBAction func podsButtonClicked(_ sender: UIButton) {
    podsButton.addBottomBorderWithColor(color: greenColor(), width: 3)
}

@IBAction func subscribedButtonClicked(_ sender: UIButton) {
    subscribedButton.addBottomBorderWithColor(color: greenColor(), width: 3)
}

@IBAction func subscribersButtonClicked(_ sender: UIButton) {
    subscribersButton.addBottomBorderWithColor(color: greenColor(), width: 3)
}

我想在每个按钮中移除其他按钮本身的边框。

【问题讨论】:

  • 方法 addBottomBorderWithColor 不是 Apple API。您应该告诉我们哪个库定义了它。建议检查他们的文档如何删除它。如果他们不这样做,请检查他们如何应用它,以便您自己删除它

标签: swift uibutton uikit


【解决方案1】:

addBottomBorderWithColor 我猜是您在项目中作为UIButton 的扩展添加的一种方法。根据它添加底部边框的方式,您必须执行以下操作之一:

如果您将按钮的底部边框添加为子视图,则需要创建一个执行以下操作的方法:

func removeBottomBorder() {
    bottomBorder.removeFromSuperview()
}

如果您将底部边框添加为子图层,则需要这样做:

func removeBottomBorder() {
    bottomBorder.removeFromSuperlayer()
}

我建议你创建一个管理底部边框的自定义按钮子类,它或多或少类似于:

class BottomBorderButton: UIButton {
    var bottomBorder: UIView? // If you are using a CALayer, use that instead

    func addBottomBorderWithColor(color: UIColor, width: Int) {
        let bottomBorder = UIView() // If you are using a CALayer, use that instead
        // Use whatever you have already implementeds here
        // ...
        self.bottomBorder = bottomBorder
    }

    func removeBottomBorder() {
        self.bottomBorder?.removeFromSuperview()
        // If you use a CALayer, use the following instead
        // self.bottomBorder?.removeFromSuperlayer()
    }
}

在您的项目中,然后在需要的地方使用 BottomBorderButton 而不是 UIButton。

【讨论】:

  • 是的,我意识到我有一个类扩展,但它不允许我存储属性。您的解决方案效果很好。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多