# How to handle multiple layer of modal in SwiftUI?

**URL:** <https://forums.swift.org/t/how-to-handle-multiple-layer-of-modal-in-swiftui/47349>\
**Category:** Using Swift\
**Tags:** off-topic, swiftui\
**Created:** [April 12, 2021, 9:35am UTC](https://forums.swift.org/t/how-to-handle-multiple-layer-of-modal-in-swiftui/47349 "2021-04-12T09:35:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mackoj](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mackoj/32/42395_2.png) [@mackoj](https://forums.swift.org/u/mackoj)\
**Post date:** [April 12, 2021, 9:35am UTC](https://forums.swift.org/t/how-to-handle-multiple-layer-of-modal-in-swiftui/47349/1 "2021-04-12T09:35:31Z")

</div>

Hi everyone,

How can we have multiple layer of modal in SwiftUI like what we can do in UIKit.

I did tried multiple approach like [these](https://forums.swift.org/t/multiple-sheet-view-modifiers-on-the-same-view/35267) without success.

SwiftUI don't process more that one `.sheet` modifier on a view.

Has anyone have any idea how to do this ?

Thanks,

![](https://global.discourse-cdn.com/swift/original/3X/1/8/183afcdad9674716a2bbfeebfd5e241eb4afb41a.gif)

> **Code**
>
> ```
> import UIKit
> import PlaygroundSupport
> 
> extension UIColor {
> class func randomColor(randomAlpha: Bool = false) -> UIColor {
> let redValue = CGFloat(arc4random_uniform(255)) / 255.0;
> let greenValue = CGFloat(arc4random_uniform(255)) / 255.0;
> let blueValue = CGFloat(arc4random_uniform(255)) / 255.0;
> let alphaValue = randomAlpha ? CGFloat(arc4random_uniform(255)) / 255.0 : 1;
> 
> return UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: alphaValue)
> }
> }
> 
> class ModalViewController: UIViewController {
>   
> override func viewDidLoad() {
> super.viewDidLoad()
> self.view.backgroundColor = UIColor.randomColor()
>     
> let button = UIButton()
> button.backgroundColor = .clear
> button.setTitle("Open", for: .normal)
> button.setTitleColor(self.view.tintColor, for: .normal)
> button.addTarget(self, action: #selector(presentModal(_:)), for: .touchUpInside)
> button.sizeToFit()
> button.center = self.view.center
> self.view.addSubview(button)
> }
>   
> @IBAction func presentModal(_ sender: UIButton) {
> let presentee = ModalViewController(nibName: nil, bundle: nil)
> self.modalPresentationStyle = .overCurrentContext
> self.modalTransitionStyle = .flipHorizontal
> self.modalPresentationCapturesStatusBarAppearance = true
> self.present(presentee, animated: true, completion: nil)
> }
> }
> 
> class ViewController: UIViewController {
> 
> override func viewDidLoad() {
> super.viewDidLoad()
> let button = UIButton()
> button.backgroundColor = .clear
> button.setTitle("Open", for: .normal)
> button.setTitleColor(self.view.tintColor, for: .normal)
> button.addTarget(self, action: #selector(presentModal(_:)), for: .touchUpInside)
> button.sizeToFit()
> button.center = self.view.center
> self.view.addSubview(button)
> }
> 
> @IBAction func presentModal(_ sender: UIButton) {
> let presentee = ModalViewController(nibName: nil, bundle: nil)
> self.modalPresentationStyle = .overCurrentContext
> self.modalTransitionStyle = .flipHorizontal
> self.modalPresentationCapturesStatusBarAppearance = true
> self.present(presentee, animated: true, completion: nil)
> }
> }
> 
> // Present the view controller in the Live View window
> PlaygroundPage.current.liveView = ViewController()
> 
> ```

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [April 12, 2021, 12:33pm UTC](https://forums.swift.org/t/how-to-handle-multiple-layer-of-modal-in-swiftui/47349/2 "2021-04-12T12:33:21Z")

</div>

This should work:

```swift
struct ModalView: View {
    @State private var showModal = false
    @State private var backgroundColor = UIColor.randomColor()

    var body: some View {
        Button("Open", action: { showModal = true })
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
            .background(Color(backgroundColor))
            .ignoresSafeArea()
            .sheet(isPresented: $showModal) { ModalView() }
    }
}

```

---

<div class="post-metadata">

**Author:** ![mackoj](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mackoj/32/42395_2.png) [@mackoj](https://forums.swift.org/u/mackoj)\
**Post date:** [April 12, 2021, 12:42pm UTC](https://forums.swift.org/t/how-to-handle-multiple-layer-of-modal-in-swiftui/47349/3 "2021-04-12T12:42:42Z")

</div>

Thanks @sveinhal it's working
