1 |
//
|
|
2 |
// ChatLayout
|
|
3 |
// RoundedCornersContainerView.swift
|
|
4 |
// https://github.com/ekazaev/ChatLayout
|
|
5 |
//
|
|
6 |
// Created by Eugene Kazaev in 2020-2022.
|
|
7 |
// Distributed under the MIT license.
|
|
8 |
//
|
|
9 |
// Become a sponsor:
|
|
10 |
// https://github.com/sponsors/ekazaev
|
|
11 |
//
|
|
12 |
|
|
13 |
import Foundation
|
|
14 |
import UIKit
|
|
15 |
|
|
16 |
/// A container view that keeps its `CustomView` masked with the corner radius provided.
|
|
17 |
public final class RoundedCornersContainerView<CustomView: UIView>: UIView {
|
|
18 |
|
|
19 |
/// Corner radius. If not provided then the half of the current view height will be used.
|
|
20 |
public var cornerRadius: CGFloat?
|
|
21 |
|
|
22 |
/// Contained view.
|
|
23 |
public lazy var customView = CustomView(frame: bounds)
|
|
24 |
|
|
25 |
/// Initializes and returns a newly allocated view object with the specified frame rectangle.
|
|
26 |
/// - Parameter frame: The frame rectangle for the view, measured in points. The origin of the frame is relative
|
|
27 |
/// to the superview in which you plan to add it.
|
|
28 |
public override init(frame: CGRect) {
|
! |
29 |
super.init(frame: frame)
|
! |
30 |
setupSubviews()
|
! |
31 |
}
|
! |
32 |
|
|
33 |
/// Returns an object initialized from data in a given unarchiver.
|
|
34 |
/// - Parameter coder: An unarchiver object.
|
|
35 |
public required init?(coder: NSCoder) {
|
! |
36 |
super.init(coder: coder)
|
! |
37 |
setupSubviews()
|
! |
38 |
}
|
! |
39 |
|
|
40 |
private func setupSubviews() {
|
! |
41 |
addSubview(customView)
|
! |
42 |
translatesAutoresizingMaskIntoConstraints = false
|
! |
43 |
insetsLayoutMarginsFromSafeArea = false
|
! |
44 |
layoutMargins = .zero
|
! |
45 |
|
! |
46 |
customView.translatesAutoresizingMaskIntoConstraints = false
|
! |
47 |
NSLayoutConstraint.activate([
|
! |
48 |
customView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
|
! |
49 |
customView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
|
! |
50 |
customView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
|
! |
51 |
customView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor)
|
! |
52 |
])
|
! |
53 |
}
|
! |
54 |
|
|
55 |
/// Lays out subviews.
|
|
56 |
public override func layoutSubviews() {
|
! |
57 |
super.layoutSubviews()
|
! |
58 |
layer.masksToBounds = false
|
! |
59 |
layer.cornerRadius = cornerRadius ?? frame.height / 2
|
! |
60 |
clipsToBounds = true
|
! |
61 |
}
|
! |
62 |
|
|
63 |
}
|
|