Skip to content

Instantly share code, notes, and snippets.

@alecsmirnov
alecsmirnov / SlidingTabContentView.swift
Created April 13, 2026 09:35
TabView alternative for use inside ScrollView
import SwiftUI
struct SlidingTabContentView<Tab1View: View, Tab2View: View>: View {
private struct SlidingTabLayout: Layout {
let selectedIndex: Int
let isTransitioning: Bool
func sizeThatFits(
proposal: ProposedViewSize,
subviews: Subviews,
import Combine
import Foundation
public extension Publisher {
/// Shares a single upstream subscription and replays the latest `count` values to new subscribers.
func share(replay count: Int) -> AnyPublisher<Output, Failure> {
// swiftlint:disable empty_count
guard count > 0 else {
return share()
.eraseToAnyPublisher()
import Foundation
extension View {
func cornerRadiusWithAnimatedChanges(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(RoundedCornerWithAnimatedChangesShape(radius: radius, corners: corners))
}
}
private struct RoundedCornerWithAnimatedChangesShape: Shape {
private var topLeftRadius: CGFloat
@alecsmirnov
alecsmirnov / UIColor+CGColor+Color+ApplicationColors.swift
Last active July 25, 2025 02:16
Extensions for quick use of app colors in places where UIColor, CGColor and Color are used
public enum ApplicationColors {
public static let red: UIColor = .red
public static let green: UIColor = .green
public static let blue: UIColor = .blue
}
// MARK: - UIColor+ApplicationColors
public extension UIColor {
static var app: ApplicationColors.Type {
ApplicationColors.self
@alecsmirnov
alecsmirnov / UITextField+phoneNumberWithSpaces.swift
Last active July 15, 2025 03:39
Handling and formatting a phone number with spaces for UITextField
private enum Constants {
static let charactersLimit = 10
}
// MARK: - UITextFieldDelegate
extension Foo: UITextFieldDelegate {
public func textField(
_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String
@alecsmirnov
alecsmirnov / SwiftGenSPMReadme.md
Last active February 7, 2025 03:18
How to install SwiftGen as an SPM package and use it as a Build Phase Script

How to install SwiftGen as an SPM package and use it as a Build Phase Script

Steps:

  • Add SwiftGen (https://github.com/SwiftGen/SwiftGen) as a dependency in Xcode -> Project -> Package Dependencies (just the executable).
  • Remove swiftgen as a library from the target under Frameworks, Libraries and Embedded Content (don't remove the package if Xcode says it's not used).
  • Add new run script phase in a Build Phases:
# echo "Running SwiftGen script"
import UIKit
private enum Constants {
static let tabHeightChangeAnimationDuration: TimeInterval = 0.2
}
final class DynamicHeightTabSwitcherView: UIView {
override var clipsToBounds: Bool {
get {
super.clipsToBounds
extension UIResponder {
var parentViewController: UIViewController? {
next as? UIViewController ?? next?.parentViewController
}
}
@alecsmirnov
alecsmirnov / CircleWithBorderViewFix.swift
Last active March 26, 2025 08:13
Fix for circle view rendering bug where the background color seeping through the border
import UIKit
final class FooView: UIView {
private var circleWithBorderViewIsConfigured = false
private let circleWithBorderView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
/// - Important: Before using on the device, need to add `Supports Document Browser: YES` line to the info.list
private enum DocumentDirectoryFile {
static func write(text: String, to filename: String = "my_test_log.txt") {
guard let folderURL = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
).last else { return }
let fileURL = folderURL.appendingPathComponent(filename)