4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[SwiftUI] NavigationViewの背景色を透明にする

Last updated at Posted at 2021-04-23

必要なもの

SwiftUI-Introspectが必要になります。 SPMでインストールしてください。

実装方法

UINavigationControllerを拡張

UINavigationController+.swift
import UIKit

extension UINavigationController {

    func clearBackgroundColor() {
        topViewController?.view.backgroundColor = .clear
        topViewController?.parent?.view.backgroundColor = .clear
        topViewController?.parent?.parent?.view.backgroundColor = .clear
    }
}

遷移元画面

UINavigationControllerの背景色を透明にします。

Hoge.swift
var body: some View {
    NavigationView {
        ZStack {
        }
    }
    .introspectNavigationController { navigationController in
        navigationController.clearBackgroundColor()
    }
}

遷移先画面

遷移先のViewControllerの背景色を透明にします。

Fuga.swift
var body: some View {
    ZStack {
    }
    .introspectViewController { viewController in
        viewController.view.backgroundColor = .clear
    }
}
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?