Photo by Croissant on Unsplash

SwiftUI already provides many native components and .contextMenu is one of them. However, unfortunately, it doesn’t have as much customization as we have in UIKit. ContextMenu only supports showing menu items with a 3D preview of the applied view but does not support showing a custom 3D preview or having a destination.

For this reason, I have created a context menu with preview component that can mimic UIKit features. As this is only a workaround for now, I took advantage of UIViewRepresentable. I hope and believe that a native preview/destination support will be available in SwiftUI 3.0 this fall.


Photo by Robert Anasch on Unsplash

Previously I wrote about AdMob banners with SwiftUI and AdMob Interstitials with SwiftUI. This time, I will write about AdMob Rewarded Ads.

Part 1: Installing AdMob

First of all, in this sample I used CocoaPods to integrate my app with AdMob. You just need to add the line below to your podfile.

pod 'Firebase/AdMob'

Next thing you would do is to add your ad identifier to your info.plist with the key GADApplicationIdentifier.

Part 2: Ad Units

The second part starts with adding your ad units in an enum so as to access them easily. For this reason, we are going to create this enum:

Part 3: Creating Rewarded Ad Loader

Unlike banners, we…


Photo by Amir Hanna on Unsplash

In a previous story I wrote about AdMob banners with SwiftUI. This time, I will write about AdMob Interstitials.

Part 1: Installing AdMob

First of all, in this sample I used CocoaPods to integrate my app with AdMob. You just need to add the line below to your podfile.

pod 'Firebase/AdMob'

Next thing you would do is to add your ad identifier to your info.plist with the key GADApplicationIdentifier.

Part 2: Ad Units

The second part starts with adding your ad units in an enum so as to access them easily. For this reason, we are going to create this enum:

Part 3: Creating Interstitial Ad Loader

Unlike banners, we need to initialize…


Photo by Darren Chan on Unsplash

If you are using Google AdMob banners in your app and would like to transition to a SwiftUI app, you would need a wrapper to show your ads in SwiftUI views. I have created this tutorial for anyone with or without AdMob banner ad integration experience.

Part 1: Installing AdMob

First of all, in this sample I used CocoaPods to integrate my app with AdMob. You just need to add the line below to your podfile.

pod 'Firebase/AdMob'

Next thing you would do is to add your ad identifier to your info.plist with the key GADApplicationIdentifier.

Part 2: Ad Units

The second part starts with adding your ad…


Photo by Karen Vardazaryan on Unsplash

Unfortunately, there is no collection view API in SwiftUI (yet?). However, by using VStack and Hstack we can create our own collection view wrapper.

If we were to write an API for collection view, we would at least need these:

private let data: [Data.Element]
private let numberOfColumns: Int
private let spacing: CGFloat
private let content: (Data.Element) -> Content

Hence, we we will initialize our view with these values.

init(_ data: Data,
numberOfColumns: Int = 2, // default is up to you
spacing: CGFloat = 8, // again here, the default value is…

If you need to present UIActivityViewController in SwiftUI, you will need to wrap this view controller with UIViewControllerRepresentable as there’s no SwiftUI API for it yet.

Doing it is quite…


Photo by delfi de la Rua on Unsplash

In SwiftUI, if you want to hide a view dynamically, you can use an if block to decide to show or not.

if shouldShow {
content
}

That means each time you need to handle a dynamically hidden view, you would need to write the code above.

But what if there was an easy way for it? ViewModifiers come to help at this point.

struct VisibilityStyle: ViewModifier {

let hidden: Bool
@ViewBuilder
func
body(content: Content) -> some View {
if hidden {
content.hidden()
} else {
content
}
}
}

And if we also create a View extension, we can…


Photo by pine watt on Unsplash

When you want to set a view’s frame in SwiftUI, there’s a single function that has multiple parameters.

frame(minWidth:idealWidth:maxWidth:
minHeight:idealHeight:maxHeight:
alignment:)

However, even if you just want to set width or height, you still need to call this function and autocompletion doesn’t really help with it.

But, by creating a View extension, we can make things easier.

extension View {   func frame(_ width: CGFloat, _ height: CGFloat) -> some View {
frame(width: width, height: height
}
func height(_ height: CGFloat) -> some View {
frame(height: height)
}
func width(_ width: CGFloat) -> some View {
frame(width: width)
}
}

By…

Cuneyt Zafer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store