To float a button over a WKWebView, add the web view to your screen's view, then add the button to the same view after it, so it sits on top. Pin the web view's top to the safe area, pin the button to the safe area's bottom and trailing edges, and give it a capsule shape with UIButton.Configuration. Moving the button inside the web view, as the 2018 post did, also works: my test showed its constraints stay active.
SWIFT TIPS · UIKIT · WEBKIT
First posted on September 11, 2018 for Swift 4, as "[WEBVIEW] WkWebview With Float Button". Rewritten on November 18, 2026. The layout test was run on a Mac as a Mac Catalyst program, which runs UIKit on macOS (not an iPhone or the simulator), with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1. The view controller was type-checked against the iOS 26.2 SDK in Swift 5 and Swift 6 modes with iOS 15 as the deployment target, but not run on a device.
In 2018 I wanted a round button floating over a web page. The post had a storyboard button, a WKWebView created in code and pinned to the screen with a 16-point gap at the top, and one line that made it float: webView.addSubview(button). The screenshot showed Google's home page with a round "X" button near the bottom.
When I rewrote this, my first guess was that moving the button into the web view would break its storyboard constraints. I tested that guess instead of writing it down, and it was wrong.
What happens when the button moves into the web view
The test builds the same layout in code: a 56-point button pinned to the bottom-right of the screen's view, then a web view added to that view, then the button moved into the web view.
before the move: button frame (314.0, 748.0, 56.0, 56.0)
inside the web view: constraints active [true, true, true, true]
button frame in the web view (314.0, 732.0, 56.0, 56.0), in the screen (314.0, 748.0, 56.0, 56.0)
after removeFromSuperview: constraints active [false, false, true, true]
All four constraints stayed active, and the button stayed in the same place on screen. Apple's documentation says addSubview(_:) removes a view from its previous superview first, and that removeFromSuperview() removes constraints that refer to the view. The test shows the difference in practice. When the button only moved one level down, from the screen's view into the web view inside it, UIKit kept the constraints. My reading is that the screen's view still contains the button, so constraints between the two still make sense. When the button was removed from the hierarchy completely, the two constraints to the screen's edges were deactivated, while its own width and height stayed.
So the 2018 approach wasn't broken. The version below gets the same result in a tidier, more explicit way.
A floating button over a web view
import UIKit
import WebKit
final class FloatingButtonWebViewController: UIViewController {
private let url: URL
private let webView = WKWebView()
private let backToTopButton: UIButton = {
var config = UIButton.Configuration.filled()
config.image = UIImage(systemName: "arrow.up")
config.cornerStyle = .capsule
let button = UIButton(configuration: config)
button.accessibilityLabel = "Back to top"
return button
}()
init(url: URL) {
self.url = url
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
webView.translatesAutoresizingMaskIntoConstraints = false
backToTopButton.translatesAutoresizingMaskIntoConstraints = false
// The button is a sibling added after the web view, so it sits on top of it.
view.addSubview(webView)
view.addSubview(backToTopButton)
let safe = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: safe.topAnchor),
webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
backToTopButton.trailingAnchor.constraint(equalTo: safe.trailingAnchor, constant: -16),
backToTopButton.bottomAnchor.constraint(equalTo: safe.bottomAnchor, constant: -16),
backToTopButton.widthAnchor.constraint(equalToConstant: 56),
backToTopButton.heightAnchor.constraint(equalTo: backToTopButton.widthAnchor),
])
backToTopButton.addTarget(self, action: #selector(backToTopTapped), for: .touchUpInside)
webView.load(URLRequest(url: url))
}
@objc private func backToTopTapped() {
let scrollView = webView.scrollView
let top = CGPoint(x: 0, y: -scrollView.adjustedContentInset.top)
scrollView.setContentOffset(top, animated: true)
}
}
What changed
- A sibling, not a child. The button is added to the screen's view after the web view, which puts it on top. Nothing depends on the web view's own subviews, and the button's constraints refer to views it's actually inside.
- The safe area. The 2018 web view's top was pinned 16 points below the top of the screen's view. In the 2018 screenshot the page sits fully visible below the navigation bar, so on that screen it worked. Pinning the web view's top to
safeAreaLayoutGuidesays what's meant directly, and the same guide keeps the button above the home indicator. Apple's documentation describes the guide as the part of the view not covered by bars and other content. - The shape. The 2018 code set
cornerRadiusto half the button's width inviewDidLoad, and in the screenshot the button is round. That relies on the button already having its final width at that point, which holds for a fixed-size button.config.cornerStyle = .capsulekeeps it round at any size without that assumption. - A label for VoiceOver. An icon-only button needs
accessibilityLabel, or VoiceOver users hear nothing useful. - An action. The original post didn't show what the button did. Here it scrolls the page back to the top, taking the scroll view's adjusted content inset into account so the page lands right below the safe area.
Things to watch with a floating button
A button that floats over content always covers something. Keep it small, put it where the page has the least to tap, and check pages that have their own bottom bar or cookie banner. In the 2018 screenshot the button sat on top of Google's location line at the bottom of the page.
A quick check for your project
- Search for
constraint(equalTo: view.topAnchoron full-screen content and check whether it should useview.safeAreaLayoutGuide.topAnchor. - Search for
cornerRadius =followed byframe.sizeorframe.widthinviewDidLoad. - Turn on VoiceOver and swipe to each floating button. It should read a name, not "button".
What the 2018 version said
The original loaded https://www.google.com with a force-unwrapped URL, made the view controller the web view's navigation delegate without implementing any delegate methods, and showed this screenshot:

Keep reading on JR Devhub
Comments