Short answer

Build the floating button with UIButton.Configuration.filled() and cornerStyle = .capsule, pin it to the bottom-trailing corner of view.safeAreaLayoutGuide with Auto Layout, and give it an accessibilityLabel because it only shows an icon. Add the shadow on the button's layer and set shadowPath once the size is known. Positioning with a frame at view.center, as the 2019 code did, puts the button's corner at the center, not the button.

SWIFT TIPS · UIKIT

First posted on July 28, 2019, as "how to make Float UIButton in programatically". Rewritten on November 17, 2026. The Swift code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target. It wasn't run on a device.

The 2019 post was a floating round button, the kind many apps use for "add" or "compose". It linked a YouTube tutorial and gave a short view controller that made a 100-point red circle with a shadow, ready to paste into a Playground. It's a nice minimal example, and it leaves out four things a real screen needs.

A floating button for a real screen

import UIKit

final class ListViewController: UIViewController {
    private let addButton = UIButton(configuration: .filled())

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground

        var config = UIButton.Configuration.filled()
        config.image = UIImage(systemName: "plus", withConfiguration: UIImage.SymbolConfiguration(pointSize: 22, weight: .semibold))
        config.cornerStyle = .capsule
        config.baseBackgroundColor = .systemRed
        addButton.configuration = config
        addButton.accessibilityLabel = "Add item"
        addButton.addAction(UIAction { _ in print("add") }, for: .touchUpInside)

        addButton.layer.shadowColor = UIColor.black.cgColor
        addButton.layer.shadowOpacity = 0.3
        addButton.layer.shadowRadius = 6
        addButton.layer.shadowOffset = CGSize(width: 0, height: 3)

        addButton.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(addButton)
        NSLayoutConstraint.activate([
            addButton.widthAnchor.constraint(equalToConstant: 60),
            addButton.heightAnchor.constraint(equalTo: addButton.widthAnchor),
            addButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
            addButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20),
        ])
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        // Tell Core Animation the shadow's shape instead of making it work it out each frame
        addButton.layer.shadowPath = UIBezierPath(ovalIn: addButton.bounds).cgPath
    }
}

What changed from 2019

Position with constraints

The 2019 code created the button with CGRect(x: view.center.x, y: view.center.y, width: 100, height: 100). A frame's x and y are its top-left corner, so the button's corner sat at the center and the button hung down and to the right of it. It also used the size of the view in viewDidLoad(), before layout, so it wouldn't follow rotation or a different screen size.

A floating action button usually lives in a corner. The code pins it 20 points from the trailing and bottom edges of the safe area, so it clears the home indicator and rounded corners on any iPhone and moves correctly on rotation. The width is fixed at 60 points and the height matches it.

A configuration instead of layer tricks

UIButton.Configuration, added in iOS 15, gives the round shape, the fill color and the icon in a few lines. cornerStyle = .capsule keeps it round at any size, so there's no cornerRadius to recalculate. The button also gets the system's pressed highlight for free, which a layer-colored button doesn't have.

A shadow that doesn't get clipped

The shadow goes on the button's own layer. The 2019 code set masksToBounds = false to keep it visible, which is the default for a button anyway. In viewDidLayoutSubviews() the code sets shadowPath to an oval of the button's size. Apple's documentation for shadowPath says that specifying an explicit path usually improves rendering performance, because Core Animation doesn't have to work out the shape from the layer's contents.

Something to read out

An icon-only button has no text for VoiceOver to read. accessibilityLabel = "Add item" fixes that. The 2019 button had no label and no action at all, so it was purely visual.

Keep it out of the way

A floating button sits on top of your content, so the last rows of a list can end up underneath it. Add bottom content inset to the scroll view, roughly the button's height plus its margin, so people can scroll the last row above it. On iPad, check that the button doesn't land in an awkward spot when the window is resized.

A quick check for your project

  1. Search for buttons positioned with view.center or fixed frames in viewDidLoad(). Rotate the device and see where they end up.
  2. Turn on VoiceOver and swipe to every icon-only button. Each should say what it does.
  3. Scroll to the bottom of any list with a floating button and check that the last row isn't hidden.

What the 2019 version said

The original set the red background and the corner radius on the layer, added a black shadow with a 0.5-point offset, full opacity and a 4-point radius, and added the button to the view. That shadow style still works. Full opacity gives a hard, dark shadow, so the version above uses 0.3 for a softer look.

Sources