EverydayIOS - SwiftUI - SwiftUI 下全屏显示弹窗

1,537 阅读1分钟

关键词: #IOS13 #Xcode11.6 #SwiftUI #fullscreen #modalPresentationStyle

问题:

*注意:技术不断在更新, 阅读前留意文章日期, 版本, 框架等信息, 免得浪费时间精力

IOS13 之后弹出窗口默认为浮在上一个页面的效果, 当然, 如果你都看到这里了, 相信你肯定是遇到了相同的问题, 也知道具体的表现是什么, 所以就不过多解释了, 来方案

SwiftUI 下的解决方案

解决思路:

  1. 先创建一个 View, 不启用的时候就通过 offset 到不可见的地方

  2. 通过某个开关去让它浮现出来

import SwiftUI

struct FullScreenViewTest: View {
	@State private var isShow = false
    var body: some View {
		ZStack {
			Button(action: {
				self.isShow.toggle()
			}) {
				Text("click")
			}
			
			ShowView()
                .offset(y: isShow ? 0 : 1000)
                .animation(.easeInOut)
                .onTapGesture {
				self.isShow.toggle() // 让 ShowView 消失 实现返回上一个页面的效果
			}
		}
    }
}

至于这个 ShowView长什么样子, 就可以单独去写了, 可以拿我写好的去测试, 代码如下:

import SwiftUI

struct ShowView: View {
    var body: some View {
		ZStack {
			Color(#colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1))
			Text("fullscreen view")
		}.edgesIgnoringSafeArea(.all)
    }
}

附录:

UIKit 下的 swift 解决方案

let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen 
self.present(vc, animated: true, completion: nil)

UIKit 下的 Objective-C 解决方案

UIViewController *appViewController = [[UIViewController alloc] init];
appViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:appViewController animated:NO completion:nil];

希望可以帮到每一个人!

如果有更好的解决方案可以在评论留言让更多人知道~ 感谢!