From 507ae078769c105ab19a9dd7d5aa61039a648844 Mon Sep 17 00:00:00 2001 From: mojganii Date: Tue, 22 Oct 2024 09:59:02 +0200 Subject: [PATCH] Fix alpha button override issues in configuration --- ios/MullvadVPN/Extensions/UIImage+Helpers.swift | 6 ++++++ ios/MullvadVPN/Views/AppButton.swift | 13 ++++++++++--- ios/MullvadVPN/Views/CustomButton.swift | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/ios/MullvadVPN/Extensions/UIImage+Helpers.swift b/ios/MullvadVPN/Extensions/UIImage+Helpers.swift index ebf11ea270a4..90d9bc7f88cc 100644 --- a/ios/MullvadVPN/Extensions/UIImage+Helpers.swift +++ b/ios/MullvadVPN/Extensions/UIImage+Helpers.swift @@ -26,4 +26,10 @@ extension UIImage { return resizedImage.withRenderingMode(renderingMode) } + + func withAlpha(_ alpha: CGFloat) -> UIImage? { + return UIGraphicsImageRenderer(size: size, format: imageRendererFormat).image { _ in + draw(in: CGRect(origin: .zero, size: size), blendMode: .normal, alpha: alpha) + } + } } diff --git a/ios/MullvadVPN/Views/AppButton.swift b/ios/MullvadVPN/Views/AppButton.swift index 0ce8de9a47e1..022bd97b130b 100644 --- a/ios/MullvadVPN/Views/AppButton.swift +++ b/ios/MullvadVPN/Views/AppButton.swift @@ -119,9 +119,10 @@ class AppButton: CustomButton { return updatedAttributeContainer } - let configurationHandler: UIButton.ConfigurationUpdateHandler = { button in - button.alpha = !button.isEnabled ? 0.5 : 1.0 + let configurationHandler: UIButton.ConfigurationUpdateHandler = { [weak self] button in + guard let self else { return } button.configuration?.baseForegroundColor = button.state.customButtonTitleColor + updateButtonBackground() } configuration = config configurationUpdateHandler = configurationHandler @@ -129,6 +130,12 @@ class AppButton: CustomButton { /// Set background image based on current style. private func updateButtonBackground() { - configuration?.background.image = style.backgroundImage + if isEnabled { + // Load the normal image and set it as the background + configuration?.background.image = style.backgroundImage + } else { + // Adjust the image for the disabled state + configuration?.background.image = style.backgroundImage.withAlpha(0.5) + } } } diff --git a/ios/MullvadVPN/Views/CustomButton.swift b/ios/MullvadVPN/Views/CustomButton.swift index 8c99ddb2cc6c..a956025f28e2 100644 --- a/ios/MullvadVPN/Views/CustomButton.swift +++ b/ios/MullvadVPN/Views/CustomButton.swift @@ -14,7 +14,7 @@ extension UIControl.State { case .normal: return UIColor.AppButton.normalTitleColor case .disabled: - return UIColor.AppButton.disabledTitleColor + return UIColor.AppButton.disabledTitleColor.withAlphaComponent(0.5) case .highlighted: return UIColor.AppButton.highlightedTitleColor default: