Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Extend a property backgroundViewColor for UIStackView to set its background color #1127

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
- Added `allQueryParameters`, `appendingQueryParameters(_:)` and `appendQueryParameters(_:)` for using `URLQueryItem`, as an addition to the `[String: String]` variants, to handle `nil`-value query parameters. [#1116](https://github.com/SwifterSwift/SwifterSwift/issues/1116) by [guykogus](https://github.com/guykogus)
- **URLSession**
- Added `dataSync(for:)` to make requests synchronously. [#1076](https://github.com/SwifterSwift/SwifterSwift/pull/1076) by [Roman Podymov](https://github.com/RomanPodymov)
- **UIStackView**
- Added `backgroundViewColor` to add background color. [#1127](https://github.com/SwifterSwift/SwifterSwift/pull/1127) by [WZBbiao](https://github.com/WZBbiao)

### Changed
- **UIButton**:
Expand Down
51 changes: 42 additions & 9 deletions Sources/SwifterSwift/UIKit/UIStackViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,39 @@
// MARK: - Initializers

public extension UIStackView {
private class BackgroundView: UIView { }

/// SwifterSwift: Add background color to UIStackView
var backgroundViewColor: UIColor? {
get {
if #available(iOS 14.0, *) {
return backgroundColor
} else {
return subviews.first(where: { $0 is BackgroundView })?.backgroundColor

Check warning on line 17 in Sources/SwifterSwift/UIKit/UIStackViewExtensions.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SwifterSwift/UIKit/UIStackViewExtensions.swift#L17

Added line #L17 was not covered by tests
}
}
set {
if #available(iOS 14.0, *) {
backgroundColor = newValue
} else {
if let existingBackgroundView = subviews.first(where: { $0 is BackgroundView }) {
existingBackgroundView.backgroundColor = newValue
} else {
let backgroundView = BackgroundView()
backgroundView.backgroundColor = newValue
insertSubview(backgroundView, at: 0)
backgroundView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
backgroundView.topAnchor.constraint(equalTo: topAnchor),
backgroundView.leadingAnchor.constraint(equalTo: leadingAnchor),
backgroundView.bottomAnchor.constraint(equalTo: bottomAnchor),
backgroundView.trailingAnchor.constraint(equalTo: trailingAnchor)
])

Check warning on line 36 in Sources/SwifterSwift/UIKit/UIStackViewExtensions.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SwifterSwift/UIKit/UIStackViewExtensions.swift#L24-L36

Added lines #L24 - L36 were not covered by tests
}
}
}
}

/// SwifterSwift: Initialize an UIStackView with an array of UIView and common parameters.
///
/// let stackView = UIStackView(arrangedSubviews: [UIView(), UIView()], axis: .vertical)
Expand All @@ -22,13 +55,13 @@
spacing: CGFloat = 0.0,
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill) {
self.init(arrangedSubviews: arrangedSubviews)
self.axis = axis
self.spacing = spacing
self.alignment = alignment
self.distribution = distribution
}

self.init(arrangedSubviews: arrangedSubviews)
self.axis = axis
self.spacing = spacing
self.alignment = alignment
self.distribution = distribution
}
/// SwifterSwift: Adds array of views to the end of the arrangedSubviews array.
///
/// - Parameter views: views array.
Expand All @@ -37,7 +70,7 @@
addArrangedSubview(view)
}
}

/// SwifterSwift: Removes all views in stack’s array of arranged subviews.
func removeArrangedSubviews() {
for view in arrangedSubviews {
Expand Down Expand Up @@ -66,7 +99,7 @@
else { return }
removeArrangedSubview(view1)
insertArrangedSubview(view1, at: view2Index)

removeArrangedSubview(view2)
insertArrangedSubview(view2, at: view1Index)
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/UIKitTests/UIStackViewExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ final class UIStackViewExtensionsTest: XCTestCase {
XCTAssertEqual(stack.arrangedSubviews.firstIndex(of: view4), 0)
waitForExpectations(timeout: 1.0)
}

func testBackgroundViewColor() {
let stack = UIStackView()
stack.backgroundViewColor = nil
XCTAssertNil(stack.backgroundViewColor)

stack.backgroundViewColor = .red
XCTAssertNotNil(stack.backgroundViewColor)

XCTAssertEqual(stack.backgroundViewColor!, UIColor.red)

if #available(iOS 14, *) {
XCTAssertEqual(stack.backgroundColor!, UIColor.red)
}
}
}

#endif