-
Notifications
You must be signed in to change notification settings - Fork 356
/
DisconnectSplitButton.swift
55 lines (44 loc) · 2.14 KB
/
DisconnectSplitButton.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// DisconnectSplitButton.swift
// MullvadVPN
//
// Created by pronebird on 29/07/2020.
// Copyright © 2020 Mullvad VPN AB. All rights reserved.
//
import Foundation
import UIKit
class DisconnectSplitButton: UIView {
let primaryButton = AppButton(style: .translucentDangerSplitLeft)
let secondaryButton = AppButton(style: .translucentDangerSplitRight)
override init(frame: CGRect) {
super.init(frame: .zero)
commonInit()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func commonInit() {
let primaryButtonBlurView = TranslucentButtonBlurView(button: primaryButton)
let secondaryButtonBlurView = TranslucentButtonBlurView(button: secondaryButton)
let stackView = UIStackView(arrangedSubviews: [primaryButtonBlurView, secondaryButtonBlurView])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.distribution = .fill
stackView.alignment = .fill
stackView.spacing = 1
let secondaryButtonSize = UIMetrics.DisconnectSplitButton.secondaryButton
addConstrainedSubviews([stackView]) {
stackView.pinEdgesToSuperview()
secondaryButton.widthAnchor.constraint(equalToConstant: secondaryButtonSize.width)
secondaryButton.heightAnchor.constraint(equalToConstant: secondaryButtonSize.height)
}
primaryButton.configuration?.contentInsets.leading += UIMetrics.DisconnectSplitButton.secondaryButton.width
// Ideally, we shouldn't need to manually resize the image ourselves.
// However, since UIButton.Configuration doesn't provide a direct property
// for controlling image scaling (like imageScaling or contentMode in other contexts),
// manual resizing has been one approach to ensure the image fits within bounds.
secondaryButton.configuration?.image = UIImage(resource: .iconReload)
.resizeImage(targetSize: secondaryButtonSize.deducting(insets: secondaryButton.defaultContentInsets))
.imageFlippedForRightToLeftLayoutDirection()
}
}