LMFeedView
Overview
The LMFeedView
is a customizable view widget that extends UIView
with additional functionality for styling, layout, and lifecycle management. It provides convenient methods for configuring appearance, managing constraints, and implementing a structured view lifecycle pattern.
File Location:
LMFeedView.swift
Functionality
UI Components
layer
: The view's layer for shadow and corner radius customizationsubviews
: The view's subviews for hierarchical layout managementsafeAreaLayoutGuide
: The view's safe area layout guide for safe area constraints
Methods
init(frame:)
: Initializes the view with a frame and sets up the lifecycletranslatesAutoresizingMaskIntoConstraints() -> Self
: Disables autoresizing mask translation and returns self for chainingroundCorners(_:with:)
: Rounds specified corners with a given corner radiusdropShadow(scale:)
: Applies a default shadow with customizable scaledropShadow(color:opacity:offSet:radius:scale:)
: Applies a custom shadow with full parameter controlpinSubView(subView:padding:)
: Pins a subview to the view's edges with optional paddingsafePinSubView(subView:padding:)
: Pins a subview to the view's safe area with optional paddingaddConstraint(top:bottom:leading:trailing:centerX:centerY:)
: Adds constraints with optional parameters for flexible layoutsetHeightConstraint(with:relatedBy:priority:)
: Sets height constraint with value or anchorsetWidthConstraint(with:relatedBy:priority:)
: Sets width constraint with value or anchor
Lifecycle Methods
setupViews()
: Override to set up subviewssetupLayouts()
: Override to set up auto layout constraintssetupAppearance()
: Override to configure visual appearancesetupActions()
: Override to set up user interactionssetupObservers()
: Override to set up observers and notifications
Customization
CustomFeedView.swift
class CustomFeedView: LMFeedView {
private let titleLabel = UILabel()
private let iconImageView = UIImageView()
override func setupViews() {
addSubview(titleLabel)
addSubview(iconImageView)
}
override func setupLayouts() {
titleLabel.translatesAutoresizingMaskIntoConstraints = false
iconImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
iconImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
iconImageView.centerYAnchor.constraint(equalTo: centerYAnchor),
iconImageView.widthAnchor.constraint(equalToConstant: 24),
iconImageView.heightAnchor.constraint(equalToConstant: 24),
titleLabel.leadingAnchor.constraint(equalTo: iconImageView.trailingAnchor, constant: 12),
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 12),
titleLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12)
])
}
override func setupAppearance() {
backgroundColor = .systemBackground
roundCorners(with: 8)
dropShadow(color: .black, opacity: 0.1, offSet: CGSize(width: 0, height: 2), radius: 4)
}
override func setupActions() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
addGestureRecognizer(tapGesture)
}
@objc private func handleTap() {
// Handle tap action
}
}
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
LMUIComponents.shared.view = CustomFeedView.self
// ...
return true
}