Short answer

Most UIView helper extensions from 2018 still compile, but several break when the view changes size or is combined with another helper. Round some corners with layer.maskedCorners instead of a mask built from bounds. Set a circle's corner radius in layoutSubviews(), not draw(_:). Put the shadow on an outer view and the clipping on an inner one, because masksToBounds = true hides the shadow. Color the status bar area with a normal view pinned to the safe area, never the private "statusBar" view.

SWIFT TIPS · UIKIT · EXTENSIONS

First posted on May 15, 2018 for Swift 4.2, as "Cool Extension Swift 4.2", with later additions. Rewritten on November 18, 2026. The view tests were run on a Mac as a Mac Catalyst program, which runs UIKit on macOS (not an iPhone or the simulator), and the Array and String helpers as a small macOS program, with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1. The new views were type-checked against the iOS 26.2 SDK in Swift 5 and Swift 6 modes with iOS 15 and iOS 26.2 as deployment targets, but not run on a device.

In 2018 I collected the small extensions I kept copying between projects into one post: a corner radius you can set in Interface Builder, a UIColor from 0 to 255 values, a maximum length for text fields, rounding only some corners, a circle view, a border view, removing duplicates from an array, a status bar background color, a drop shadow, and splitting a string into groups. Ten helpers, each with a "How to Use".

Every one of them looks fine on its own. I ran them on a Mac: four misbehaved as soon as a view resized or two helpers met, and one depends on code the post doesn't include.

What the 2018 helpers did when tested

The test program contains the 2018 extensions as posted, minus a stray closing brace, and checks each one:

1. maxLength: target-action pairs after setting it twice: 1
   text field after the screen let go of it: still in memory, dictionary holds 1
   maxLength on a field where it was never set: 3
2. roundCornersView: mask path 100 pt wide on a 300 pt view; pixel at x 50: black, at x 200: white (hidden)
3. CircleView: radius 25 at 50 pt, 25 after resizing to 120 pt (a circle needs 60)
   layer has a drawn bitmap: CircleView true, plain UIView false
4. radius first: masksToBounds false, pixel 3 pt left of the card: shadow
4. shadow first: masksToBounds true, pixel 3 pt left of the card: no shadow
  1. maxLength kept every text field alive. The 2018 version stored the limits in a global dictionary keyed by the text field, so the dictionary held a strong reference to each one. After the screen let go of the field, it stayed in memory. The UITextField length post linked below covers a limit that also handles paste, emoji and Japanese input.
  2. The rounded-corner mask stayed at its old size. roundCornersView built a mask from bounds at the moment it was called. When the view grew from 100 to 300 points wide, the mask stayed 100 wide and hid the rest of the view.
  3. CircleView stopped being a circle. It set the corner radius in draw(_:), which doesn't run again when only the size changes. At 120 points it kept the 25-point radius from its 50-point size. Overriding draw(_:) also gave the layer a bitmap to draw into, which a plain view doesn't need.
  4. The corner radius and the shadow fought. The 2018 cornerRadius setter turned on masksToBounds, and dropShadow() turned it off. Whichever ran last won. Radius first, then shadow: the shadow showed, but subviews weren't clipped to the corners. Shadow first, then radius: the shadow was gone.

The one I couldn't test is the status bar color, item 8. Its "How to Use" calls UIApplication.shared.statusBarView and says to "use extention UIApplication above", but no such extension appears in the post, so I didn't test a version of it. Apple's documentation for UIStatusBarManager says it describes the status bar's configuration, that you don't use it to change the bar, and that settings such as visibility go through each view controller. Coloring the area behind the bar with your own view, as below, needs no status bar access at all.

Helpers that keep working when the size changes

import UIKit

extension UIColor {
    /// Color from 0-255 values, like the numbers a design tool shows.
    convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1) {
        self.init(red: r / 255, green: g / 255, blue: b / 255, alpha: a)
    }
}

extension UIView {
    /// Rounds only some corners. Core Animation keeps them right when the view changes size.
    func roundCorners(_ corners: CACornerMask, radius: CGFloat) {
        layer.cornerRadius = radius
        layer.maskedCorners = corners
        layer.cornerCurve = .continuous
    }
}

/// Round corners and a shadow together: the shadow sits on this view's layer,
/// and the clipping happens one level down, in contentView.
final class CardView: UIView {
    let contentView = UIView()
    var cornerRadius: CGFloat = 12 { didSet { setNeedsLayout() } }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUp()
    }

    private func setUp() {
        backgroundColor = .clear
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.15
        layer.shadowRadius = 6
        layer.shadowOffset = CGSize(width: 0, height: 2)

        contentView.backgroundColor = .secondarySystemGroupedBackground
        contentView.clipsToBounds = true
        contentView.frame = bounds
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(contentView)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        contentView.layer.cornerRadius = cornerRadius
        contentView.layer.cornerCurve = .continuous
        // A shadow path saves Core Animation from working out the shape from the content.
        layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
    }
}

/// A circle at any size: the radius is set whenever the size changes.
final class CircleView: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()
        layer.cornerRadius = min(bounds.width, bounds.height) / 2
    }
}

/// A one-point border, set once when the view is created (in code or from a storyboard).
final class BorderedView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUp()
    }

    private func setUp() {
        layer.borderWidth = 1
        layer.borderColor = UIColor(r: 117, g: 183, b: 46).cgColor
    }
}

extension UIViewController {
    /// Colors the area behind the status bar with a normal view, no private API.
    @discardableResult
    func addStatusBarBackground(_ color: UIColor) -> UIView {
        let bar = UIView()
        bar.backgroundColor = color
        bar.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bar)
        NSLayoutConstraint.activate([
            bar.topAnchor.constraint(equalTo: view.topAnchor),
            bar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            bar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            bar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        ])
        return bar
    }
}

The same checks with the new code:

1. roundCorners, 100 pt then 300 pt wide: mask layer none, cornerRadius 10, pixel at x 200: black (visible)
2. CircleView: radius 25 at 50 pt, 60 at 120 pt, drawn bitmap: false
3. CardView: top-left content pixel clipped by the corner, pixel 3 pt below the card shadow, shadowPath set: true

How each one works

  • Some corners: maskedCorners. roundCorners(_:radius:) sets the layer's corner radius and lists the corners it applies to, for example only the bottom two. There's no mask layer, so there's nothing to go stale when the view resizes, and the view stayed fully visible at 300 points.
  • A circle: layoutSubviews(). It runs on every layout pass, including the one after a size change, so the radius is always half of the shorter side. At 120 points the radius was 60, and the layer had no drawn bitmap.
  • Corners and a shadow: two views. CardView puts the shadow on its own layer and clips its contentView. The test put a black square in the content's top-left corner: it was clipped by the rounded corner, and the shadow still showed below the card.
  • shadowPath. Apple's documentation says that specifying an explicit shadow path usually improves rendering performance, because the layer doesn't have to work out the shadow's shape from its content. The path is updated in layoutSubviews() so it follows the size.
  • A border: set once. BorderedView sets its border in both initializers, so it works from code and from a storyboard. The 2018 CustomViewBorder set it in draw(_:), which had the same extra-bitmap cost as CircleView.
  • The status bar area: a normal view. addStatusBarBackground(_:) adds a view from the top of the screen down to the top of the safe area. It sits behind the status bar and goes away with the screen.
  • UIColor(r:g:b:). Unchanged except for an optional alpha. For colors that should change in dark mode, named colors in the asset catalog are the better home.

The ones that were fine

The Array and String helpers, items 7 and 10, are plain Swift and still work. Run as a small program:

removeDuplicates: ["a", "b", "c", "e", "f"]
"TestData".group(of: 4): ["Test", "Data"]
"TestData1".group(of: 4): ["Test", "Data", "1"]

removeDuplicates() keeps the first copy of each value in its original order, which a Set wouldn't. group(of:) works on characters, so the last group is just shorter when the length doesn't divide evenly. Both have their own posts with more options, linked below.

Two lines in the post don't compile as written. The usage example prints dataTab, a name that doesn't exist (the array was dataArray), and the rounded-corner extension is followed by an extra }. Type-checked with Xcode 26.2:

o017_2018.swift:12:1: error: extraneous '}' at top level
o017_2018.swift:29:11: error: cannot find 'dataTab' in scope

A quick check for your project

  1. Search for UIBezierPath(roundedRect: self.bounds used for a layer mask. If the view can ever change size, switch to maskedCorners.
  2. Search for override func draw(_ rect: CGRect) that only sets layer properties. Move those lines to an initializer or layoutSubviews().
  3. Search for value(forKey: "statusBar") and statusBarWindow. Both reach into private UIKit views.
  4. Search for global dictionaries keyed by views, like [UITextField: Int]. Each one keeps its views alive.

What the 2018 version said

The post showed the Interface Builder side of the helpers: the Corner Radius field the @IBInspectable property adds to the Attributes inspector, the Max Length field it adds to text fields, a view's class set to CircleView, and the green status bar result from item 8:

The Attributes inspector with a Corner Radius field added by IBInspectable

The Attributes inspector for a text field with a Max Length field

The Identity inspector with the view's class set to CircleView

A green status bar area at the top of the 2018 app

Sources