-
-
Save Ibrahimhass/e121f55dcc87dc90bf23dc43b06e7ee5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Converted by Storyboard to SwiftUI Converter - https://swiftify.com/converter/storyboard2swiftui | |
import SwiftUI | |
import WebKit | |
// -------------------------------------------------------------------------------- | |
// ViewController | |
// -------------------------------------------------------------------------------- | |
struct View1: View { | |
var webView: WKWebView { | |
let configuration = WKWebViewConfiguration() | |
configuration.allowsInlineMediaPlayback = true | |
configuration.dataDetectorTypes = [] | |
configuration.mediaTypesRequiringUserActionForPlayback = .all | |
configuration.preferences.javaScriptCanOpenWindowsAutomatically = true | |
let webView = WKWebView(frame: .zero, configuration: configuration) | |
return webView | |
} | |
var webView2: WKWebView { | |
let configuration = WKWebViewConfiguration() | |
configuration.suppressesIncrementalRendering = true | |
let webView = WKWebView(frame: .zero, configuration: configuration) | |
webView.allowsBackForwardNavigationGestures = true | |
webView.customUserAgent = "user agent" | |
return webView | |
} | |
var body: some View { | |
ZStack(alignment: .topLeading) { | |
GeometryReader { geometry in | |
VStack() { | |
WebView(webView: webView, url: URL(string: "")) | |
.frame(dynamicWidth: 414, dynamicHeight: 818) | |
WebView(webView: webView2, url: URL(string: "")) | |
.frame(dynamicWidth: 414, dynamicHeight: 0) | |
.isHidden(true, remove: true) | |
} | |
.frame(dynamicWidth: 414, dynamicHeight: 818) | |
.offset(dynamicX: 0, dynamicY: 44) | |
} | |
} | |
.frame(dynamicWidth: 414, dynamicHeight: 896) | |
.background(Color(.systemBackground)) | |
.edgesIgnoringSafeArea(.all) | |
} | |
} | |
struct View1_Previews: PreviewProvider { | |
static var previews: some View { | |
View1() | |
.previewDevice(PreviewDevice(rawValue: "iPhone 11")) | |
.previewInterfaceOrientation(.portrait) | |
.preferredColorScheme(.light) | |
} | |
} | |
// -------------------------------------------------------------------------------- | |
// SwiftUI View Extension | |
// -------------------------------------------------------------------------------- | |
extension View { | |
/// Hide or show the view based on a boolean value. | |
/// | |
/// Example for visibility: | |
/// ``` | |
/// Text("Label") | |
/// .isHidden(true) | |
/// ``` | |
/// | |
/// Example for complete removal: | |
/// ``` | |
/// Text("Label") | |
/// .isHidden(true, remove: true) | |
/// ``` | |
/// | |
/// - Parameters: | |
/// - hidden: Set to `false` to show the view. Set to `true` to hide the view. | |
/// - remove: Boolean value indicating whether or not to remove the view. | |
@ViewBuilder func isHidden(_ hidden: Bool, remove: Bool = false) -> some View { | |
if hidden { | |
if !remove { | |
self.hidden() | |
} | |
} else { | |
self | |
} | |
} | |
} | |
// -------------------------------------------------------------------------------- | |
// WebView | |
// -------------------------------------------------------------------------------- | |
struct WebView: UIViewRepresentable { | |
var webView = WKWebView() | |
var url: URL? | |
func makeUIView(context: Context) -> WKWebView { | |
if let url = self.url { | |
let request = URLRequest(url: url) | |
webView.load(request) | |
} | |
return webView | |
} | |
func updateUIView(_ webView: WKWebView, context: Context) { } | |
} | |
// -------------------------------------------------------------------------------- | |
// Dynamic Size Helper | |
// -------------------------------------------------------------------------------- | |
struct DynamicSize { | |
static private let baseViewWidth: CGFloat = 414.0 | |
static private let baseViewHeight: CGFloat = 896.0 | |
static func getHeight(_ height: CGFloat) -> CGFloat { | |
return (height / baseViewHeight) * UIScreen.main.bounds.height | |
} | |
static func getWidth(_ width: CGFloat) -> CGFloat { | |
return (width / baseViewWidth) * UIScreen.main.bounds.width | |
} | |
static func getOffsetX(_ x: CGFloat) -> CGFloat { | |
return (x / baseViewWidth) * UIScreen.main.bounds.width | |
} | |
static func getOffsetY(_ y: CGFloat) -> CGFloat { | |
return (y / baseViewHeight) * UIScreen.main.bounds.height | |
} | |
} | |
// -------------------------------------------------------------------------------- | |
// Frame and Offset Helper | |
// -------------------------------------------------------------------------------- | |
extension View { | |
func frame(dynamicWidth: CGFloat? = nil, dynamicHeight: CGFloat? = nil, alignment: Alignment = .center) -> some View { | |
frame( | |
width: DynamicSize.getWidth(dynamicWidth ?? 0), | |
height: DynamicSize.getHeight(dynamicHeight ?? 0), | |
alignment: alignment) | |
} | |
func offset(dynamicX: CGFloat = 0, dynamicY: CGFloat = 0) -> some View { | |
offset(x: DynamicSize.getOffsetX(dynamicX), y: DynamicSize.getOffsetY(dynamicY)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment