Skip to content

Commit c4565bf

Browse files
authored
Add UIView.addBottomSeparator extension 🚀 (#1234)
1 parent 24b2b49 commit c4565bf

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
44

55
## Upcoming Release
66
### Added
7+
- **UIView**
8+
- Added `addBottomSeparator(color:height:spacing:)` method to easily insert a bottom line to a view. 🚀 [#1234](https://github.com/SwifterSwift/SwifterSwift/pull/1234) by [sangjin](https://github.com/SsangG77)
79
- **Android**
810
- Added Android platform support. [#1230](https://github.com/SwifterSwift/SwifterSwift/pull/1230) by [Marc Prud'hommeaux](https://github.com/marcprux)
911

Sources/SwifterSwift/UIKit/UIViewExtensions.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,29 @@ public extension UIView {
700700
}
701701
return views
702702
}
703+
704+
/// SwifterSwift: Adds a horizontal separator to the bottom of the view.
705+
///
706+
/// let view = UIView()
707+
/// view.addBottomSeparator(color: .lightGray, height: 1, spacing: 8)
708+
///
709+
/// - Parameters:
710+
/// - color: The separator color. Default is `.black`.
711+
/// - height: The height of the separator. Default is `1`.
712+
/// - spacing: Spacing from the bottom edge. Default is `0`.
713+
func addBottomSeparator(color: UIColor = .black, height: CGFloat = 1, spacing: CGFloat = 0) {
714+
let line = UIView()
715+
line.translatesAutoresizingMaskIntoConstraints = false
716+
line.backgroundColor = color
717+
line.layer.cornerRadius = height / 2
718+
self.addSubview(line)
719+
NSLayoutConstraint.activate([
720+
line.heightAnchor.constraint(equalToConstant: height),
721+
line.leadingAnchor.constraint(equalTo: leadingAnchor),
722+
line.trailingAnchor.constraint(equalTo: trailingAnchor),
723+
line.bottomAnchor.constraint(equalTo: bottomAnchor, constant: spacing)
724+
])
725+
}
703726
}
704727

705728
// MARK: - Constraints

Tests/UIKitTests/UIViewExtensionsTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,20 @@ final class UIViewExtensionsTests: XCTestCase { // swiftlint:disable:this type_b
683683
view.removeBlur()
684684
XCTAssertFalse(view.subviews.first is UIVisualEffectView)
685685
}
686+
687+
func testAddBottomSeparator() throws {
688+
let view = UIView()
689+
let expectedColor = UIColor.red
690+
let expectedHeight: CGFloat = 2.0
691+
692+
view.addBottomSeparator(color: expectedColor, height: expectedHeight)
693+
694+
let separator = try XCTUnwrap(view.subviews.first, "Separator not found")
695+
696+
XCTAssertEqual(view.subviews.count, 1)
697+
XCTAssertEqual(separator.backgroundColor, expectedColor)
698+
XCTAssertEqual(separator.frame.height, expectedHeight)
699+
}
686700
}
687701

688702
#endif

0 commit comments

Comments
 (0)