ã²ã¨ã¤ã®Viewããè¤æ°ã®ç»é¢ãã¢ã¼ãã«ã¸é·ç§»ãããããã¿ã³ãè¤æ°ç¨æãã¦ããããã対å¿ããç»é¢ãã¢ã¼ãã«è¡¨ç¤ºããããã¨ããããã¾ããããªãã
.sheet()
modifierãè¨å®ããã°ãã¢ã¼ãã«è¡¨ç¤ºã§ãããã¨ã¯ç¥ã£ã¦ããããããããç°ãªãè¤æ°ã®ç»é¢ã¸ã®é·ç§»ã®å ´åã¯ãã¾ããããªãã
å®è¡ç°å¢
- Xcode 11.6
- iOS 13.6
åé¡ã®æå
ã²ã¨ã¤ã®ç»é¢ä¸ã«ãç»åãé¸æãããã¿ã³ãã¨ãç»åãåãåºã(ã¯ãããã³ã°ãã)ãã¿ã³ããç¨æããã
ããããã®ãã¿ã³ãæ¼ããæã«ã対å¿ããç»é¢ãã¢ã¼ãã«è¡¨ç¤ºããããã
import SwiftUI struct SampleView : View { @State private var sheetIsPresented1 = false @State private var sheetIsPresented2 = false var body: some View { VStack { Button(action: { self.sheetIsPresented1 = true }, label: { Text("modal 1 (show picker)") }) Button(action: { self.sheetIsPresented2 = true }, label: { Text("modal 2 (show cropper)") }) } .sheet(isPresented: $sheetIsPresented1) { ImagePickerView(sourceType: .photoLibrary, onCanceled: { // on cancel }) { (image) in // on success } } .sheet(isPresented: $sheetIsPresented2) { ImageCropView(originalImage: UIImage(), onCanceled: { // on cancel }) { (image) in // on success } } } }
modal 2ãã¿ã³ã¯ãã¾ãåå¿ããããmodal 1ãã¿ã³ãæ¼ãã¦ãåå¿ããªããªã£ã¦ãã¾ã£ããã©ãããã°è§£æ±ºã§ããã®ãï¼
解決編
解決æ¹æ³ã¯ãµãã¤ããã
ãã¿ã³ã«ãããã .sheet() ã¡ã½ãããç´ã¥ãã
ãã¿ã³ã«å¯¾ãã¦ãå¦çãæ確ã«ãªãã®ã§å人çã«ã¯å¥½ãã ããbodyããããã£ããã¡ããã¡ããã¦ãã¾ããã¡ã
import SwiftUI struct SampleView : View { @State private var sheetIsPresented1 = false @State private var sheetIsPresented2 = false var body: some View { VStack { Button(action: { self.sheetIsPresented1 = true }, label: { Text("modal 1 (show picker)") }) .sheet(isPresented: $sheetIsPresented1) { ImagePickerView(sourceType: .photoLibrary, onCanceled: { // on cancel }) { (image) in // on success } } Button(action: { self.sheetIsPresented2 = true }, label: { Text("modal 2 (show cropper)") }) .sheet(isPresented: $sheetIsPresented2) { ImageCropView(originalImage: UIImage(), onCanceled: { // on cancel }) { (image) in // on success } } } } }
.sheet()ã ä¸ç®æã§å®ç¾©ããå ´å
ã©ããªã·ã¼ãããæ示ãã¦ããå¿ è¦ãããããå¦çããå ´æãã¾ã¨ãã¦ãããã¨ãã§ããããã¨ãã°ãã¢ã¼ãã«Aâã¢ã¼ãã«Bãªã©é£ç¶ãã¦ç»é¢ãå¤ãããå ´åãªã©ããã¿ã³ã«ç´ä»ããªãå¦çããã®æ¹æ³ã§ãªãã¹ã ã¼ãºã«å®è£ ã§ããã
struct SampleView : View { enum SheetType { case imagePick case imageCrop } @State private var currentSheet: SheetType = .imagePick @State private var sheetIsPresented = false var body: some View { VStack { Button(action: { self.currentSheet = .imagePick self.sheetIsPresented = true }, label: { Text("modal 1 (show picker)") }) Button(action: { self.currentSheet = .imageCrop self.sheetIsPresented = true }, label: { Text("modal 2 (show cropper)") }) } .sheet(isPresented: $sheetIsPresented) { if (self.currentSheet == .imagePick) { ImagePickerView(sourceType: .photoLibrary, onCanceled: { // on cancel }) { (image) in // on success } } else if (self.currentSheet == .imageCrop) { ImageCropView(originalImage: UIImage(), onCanceled: { // on cancel }) { (image) in // on success } } } } }
以ä¸ã§ãã²ã¨ã¤ã®Viewããè¤æ°ã®ã·ã¼ãã表示ãããã¨ãã§ããããã«ãªã£ãã