Skip to main content

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 customization
  • subviews: The view's subviews for hierarchical layout management
  • safeAreaLayoutGuide: The view's safe area layout guide for safe area constraints

Methods

  • init(frame:): Initializes the view with a frame and sets up the lifecycle
  • translatesAutoresizingMaskIntoConstraints() -> Self: Disables autoresizing mask translation and returns self for chaining
  • roundCorners(_:with:): Rounds specified corners with a given corner radius
  • dropShadow(scale:): Applies a default shadow with customizable scale
  • dropShadow(color:opacity:offSet:radius:scale:): Applies a custom shadow with full parameter control
  • pinSubView(subView:padding:): Pins a subview to the view's edges with optional padding
  • safePinSubView(subView:padding:): Pins a subview to the view's safe area with optional padding
  • addConstraint(top:bottom:leading:trailing:centerX:centerY:): Adds constraints with optional parameters for flexible layout
  • setHeightConstraint(with:relatedBy:priority:): Sets height constraint with value or anchor
  • setWidthConstraint(with:relatedBy:priority:): Sets width constraint with value or anchor

Lifecycle Methods

  • setupViews(): Override to set up subviews
  • setupLayouts(): Override to set up auto layout constraints
  • setupAppearance(): Override to configure visual appearance
  • setupActions(): Override to set up user interactions
  • setupObservers(): 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
}