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 2 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
38 changes: 38 additions & 0 deletions Sources/SwifterSwift/UIKit/UIStackViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ import UIKit
// 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 {
for subview in subviews {
if subview is BackgroundView {
return subview.backgroundColor
}
}
return nil
WZBbiao marked this conversation as resolved.
Show resolved Hide resolved
}
}
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)
])
}
}
}
}

/// SwifterSwift: Initialize an UIStackView with an array of UIView and common parameters.
///
/// let stackView = UIStackView(arrangedSubviews: [UIView(), UIView()], axis: .vertical)
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