Khoa Pham
Khoa Pham

Ohayo

Swift Discovery

Discover all the tech

Featured

My year in review 2020

Issue #715

I remember this time last year in December 2019, I spent almost every single bit of my free time on Puma because I want a Swift friendly version of fastlane that suits my need and leverages Swift 5 features.

Here’s my review of my …

How to make tooltip in SwiftUI for macOS

Issue #617

On macOS 11, we can use .help modifier to add tooltip

Button()
    .help("Click here to open settings")

If you support macOS 10.15, then create empty NSView and use as overlay. Need to updateNSView in case we toggle the state of …

How to make translucent SwiftUI List in macOS

Issue #615

List {
    ForEach(books) { (book: Book) in
        BookRow(book: book)
    }
}
.listStyle(SidebarListStyle())  

How to make tab view in SwiftUI

Issue #614

struct MyTabView: View {
    @EnvironmentObject
    var preferenceManager: PreferenceManager

    var body: some View {
        VOrH(isVertical: preferenceManager.preference.position.isVertical) {
            OneTabView(image: …

How to return VStack or HStack in SwiftUI

Issue #613

struct VOrH<Content>: View where Content: View {
    let isVertical: Bool
    let content: () -> Content

    init(isVertical: Bool, @ViewBuilder content: @escaping () -> Content) {
        self.isVertical = isVertical …

How to present NSWindow modally

Issue #612

Use runModal

This method runs a modal event loop for the specified window synchronously. It displays the specified window, makes it key, starts the run loop, and processes events for that window. (You do not need to show the window …

How to use Picker with enum in SwiftUI

Issue #611

enum WindowPosition: String, CaseIterable {
    case left
    case top
    case bottom
    case right
}
Picker(selection: $preference.position, label: Text("Position")) {
    ForEach(WindowPosition.allCases, id: \.self) { …

How to use visual effect view in NSWindow

Issue #610

Set NSVisualEffectView as contentView of NSWindow, and our main view as subview of it. Remember to set frame or autoresizing mask as non-direct content view does not get full size as the window

let mainView = MainView()
    .environment(\. …

How to animate NSWindow

Issue #609

Use animator proxy and animate parameter

var rect = window.frame
rect.frame.origin.x = 1000
NSAnimationContext.runAnimationGroup({ context in
    context.timingFunction = CAMediaTimingFunction(name: .easeIn)
    window.animator().setFrame( …

How to find active application in macOS

Issue #608

An NSRunningApplication instance for the current application.

NSRunningApplication.current

The running app instance for the app that receives key events.

NSWorkspace.shared.frontmostApplication

 

How to compare for nearly equal in Swift

Issue #607

Implement Equatable and Comparable and use round

struct RGBA: Equatable, Comparable {
    let red: CGFloat
    let green: CGFloat
    let blue: CGFloat
    let alpha: CGFloat

    init(_ red: CGFloat, _ green: CGFloat, _ blue: CGFloat, _ …

How to conform to Hashable for class in Swift

Issue #606

Use ObjectIdentifier

A unique identifier for a class instance or metatype.

final class Worker: Hashable {
    static func == (lhs: Worker, rhs: Worker) -> Bool {
        return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
    } …

How to edit selected item in list in SwiftUI

Issue #605

I use custom TextView in a master detail application.

import SwiftUI

struct TextView: NSViewRepresentable {
    @Binding var text: String

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeNSView …

How to log in SwiftUI

Issue #604

I see that the modifier needs to do something on the content, otherwise it is not getting called! This logs on the modifier, when the View is created. A View won’t be recreated unless necessary

struct LogModifier: ViewModifier { …

How to mask with UILabel

Issue #603

Need to set correct frame for mask layer or UILabel, as it is relative to the coordinate of the view to be masked

let aView = UIView(frame: .init(x: 100, y: 110, width: 200, height: 100))

let textLayer = CATextLayer()
textLayer. …

How to avoid pitfalls in SwiftUI

Issue #602

Identify by unique id

ForEach(store.blogs.enumerated().map({ $0 }), id: \.element.id) { index, blog in

}
```

## 

How to use application will terminate in macOS

Issue #601

On Xcode 11, applicationWillTerminate is not called because of default automatic termination on in Info.plist. Removing NSSupportsSuddenTermination to trigger will terminate notification

func applicationWillTerminate(_ notification: …

How to sync multiple CAAnimation

Issue #600

Use same CACurrentMediaTime

final class AnimationSyncer {
    static let now = CACurrentMediaTime()

    func makeAnimation() -> CABasicAnimation {
        let animation = CABasicAnimation(keyPath: "opacity")
        animation. …

How to use TabView with enum in SwiftUI

Issue #599

Specify tag

enum Authentication: Int, Codable {
    case key
    case certificate
}


TabView(selection: $authentication) {
    KeyAuthenticationView()
        .tabItem {
            Text("🔑 Key")
        }
        .tag( …

How to build SwiftUI style UICollectionView data source in Swift

Issue #598

It’s hard to see any iOS app which don’t use UITableView or UICollectionView, as they are the basic and important foundation to represent data. UICollectionView is very basic to use, yet a bit tedious for common use cases, but …

How to make round border in SwiftUI

Issue #597

TextView(font: R.font.text!, lineCount: nil, text: $text, isFocus: $isFocus)
.padding(8)
.background(R.color.inputBackground)
.cornerRadius(10)
.overlay(
    RoundedRectangle(cornerRadius: 10)
        .stroke(isFocus ? R.color. …

Khoa Pham

Hello, I’m Khoa

I’m a thinker and storyteller with a passion for exploring the intersection of creativity and technology

🧑‍💻 I love crafting high quality and useful apps
🔥 I love open source. My GitHub open source has 2.3k followers with packages that are integrated by 45k+ apps and over 3.4m+ downloads on CocoaPods.
✍️ I write here on my blog and on Medium, which has over 2.7k+ followers with tons of articles and 90k+ monthly views.
🖥 Follow me for sharings about Swift, SwiftUI, iOS and macOS development.
Hei