Slather logo

Coverage for "RoundedCornersContainerView.swift" : 0.00%

(0 of 26 relevant lines covered)

ChatLayout/Classes/Extras/RoundedCornersContainerView.swift

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