Skip to content

kevinhermawan/ViewCondition

Repository files navigation

ViewCondition

An extension to the View protocol that provides conditional view modifiers.

Overview

ViewCondition enhances SwiftUI's conditional rendering capabilities, makes it easier for developers to create adaptive and responsive user interfaces. With ViewCondition, you can easily apply modifiers, control visibility, or hide views based on various conditions such as boolean logic, operating system, or module availability. This flexibility allows you to tailor your app's interface for different platforms and user needs without complex conditional statements.

Documentation

You can find the documentation here: https://kevinhermawan.github.io/ViewCondition/documentation/viewcondition

Installation

You can add ViewCondition as a dependency to your project using Swift Package Manager by adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/kevinhermawan/ViewCondition.git", .upToNextMajor(from: "2.0.0"))
]

Alternatively, in Xcode:

  1. Open your project in Xcode.
  2. Click on File -> Swift Packages -> Add Package Dependency...
  3. Enter the repository URL: https://github.com/kevinhermawan/ViewCondition.git
  4. Choose the version you want to add. You probably want to add the latest version.
  5. Click Add Package.

Usage

If Modifiers

The if modifiers allow you to conditionally apply modifications to a view.

Basic Boolean Condition

Text("Hello, World!")
    .if(someCondition) { view in
        view.foregroundColor(.red)
    }

Operating System Specific

Text("Hello, World!")
    .if(os: .iOS) { view in
        view.padding()
    }

Module Availability

Text("Hello, World!")
    .if(canImport: .uiKit) { view in
        view.foregroundColor(.blue)
    }

Multiple Boolean Conditions

Text("Hello, World!")
    .if([condition1, condition2, condition3]) { view in
        view.bold()
    }

Multiple Operating Systems

Text("Hello, World!")
    .if(os: [.iOS, .macOS]) { view in
        view.font(.largeTitle)
    }

Multiple Module Availabilities

Text("Hello, World!")
    .if(canImport: [.uiKit, .appKit]) { view in
        view.italic()
    }

Visible Modifiers

The visible modifiers control the visibility of a view.

Basic Boolean Condition

Text("I'm visible!")
    .visible(if: someCondition)

With Remove Option

Text("I might be removed")
    .visible(if: someCondition, removeCompletely: true)

Operating System Specific

Text("iOS Only")
    .visible(on: .iOS)

Module Availability

Text("UIKit Available")
    .visible(ifCanImport: .uiKit)

Multiple Boolean Conditions

Text("All conditions must be true")
    .visible(if: [condition1, condition2, condition3])

Multiple Operating Systems

Text("iOS or macOS")
    .visible(on: [.iOS, .macOS])

Multiple Module Availabilities

Text("UIKit or AppKit")
    .visible(ifCanImport: [.uiKit, .appKit])

Hide Modifiers

The hide modifiers control the hiding of a view.

Basic Boolean Condition

Text("I'm hidden!")
    .hide(if: someCondition)

With Remove Option

Text("I might be removed")
    .hide(if: someCondition, removeCompletely: true)

Operating System Specific

Text("Hidden on iOS")
    .hide(on: .iOS)

Module Availability

Text("Hidden if UIKit is available")
    .hide(ifCanImport: .uiKit)

Multiple Boolean Conditions

Text("Hidden if all conditions are true")
    .hide(if: [condition1, condition2, condition3])

Multiple Operating Systems

Text("Hidden on iOS or macOS")
    .hide(on: [.iOS, .macOS])

Multiple Module Availabilities

Text("Hidden if UIKit or AppKit is available")
    .hide(ifCanImport: [.uiKit, .appKit])

License

MIT License