为什么UIViews的背景颜色没有更新?

时间:2018-05-17 12:04:14

标签: swift uiview

Switch语句有效,但不会重置视图背景颜色等。

我在UIImage(自定义类型UIButton)中嵌入了UIView(图标)和DropShadowCircleView,如下图所示。

buttons

点按行走按钮时,var navigationOption设置为walkingdriving并执行setupNavigationSelectionView()

问题:开关的情况“行走”工作正常,但案例“驾驶”不会重置{{1和图标色调颜色witcback到他们的原始设置,例如;背景颜色等..任何想法为什么?

UIView

编辑:这是我的func setupNavigationSelectionView(){ switch navigationOption { case "walking": walkingBg.setGradientBackground(colourOne: softGreen, ColourTwo: softBlue) walkingBg.layer.cornerRadius = walkingBg.frame.width / 2 walkingBg.clipsToBounds = true walkingIcon.tintColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) case "driving": walkingBg.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) walkingBg.layer.cornerRadius = walkingBg.frame.width / 2 walkingBg.clipsToBounds = true walkingIcon.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) default: break } } 班级

DropShadowCircleView

编辑:这是我的class DropShadowCircleView: UIView { override func awakeFromNib() { setupView() super.awakeFromNib() } func setupView(){ self.layer.shadowOpacity = 0.50 self.layer.shadowRadius = 20 self.layer.shadowColor = UIColor.black.cgColor self.layer.cornerRadius = self.frame.width / 2 } } 函数,该函数位于setGradientBackground

的扩展文件中
UIView

1 个答案:

答案 0 :(得分:0)

重置图标时需要删除渐变图层。

将此添加到您的extension UIView

func removeGradientBackground() {
    guard
        let idx = layer.sublayers?.index(where: { $0 is CAGradientLayer })
        else { return }

    layer.sublayers?.remove(at: idx)
}

并在重置图标时调用它。