• Stars
    star
    2,157
  • Rank 20,521 (Top 0.5 %)
  • Language
    Swift
  • Created about 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

UIKit a-là SwiftUI.framework [min deployment target iOS10]

Render Swift ObjC++ License

Render

CoreRender is a SwiftUI inspired API for UIKit (that is compatible with iOS 10+ and ObjC).

Introduction

  • Declarative: CoreRender uses a declarative API to define UI components. You simply describe the layout for your UI based on a set of inputs and the framework takes care of the rest (diff and reconciliation from virtual view hierarchy to the actual one under the hood).
  • Flexbox layout: CoreRender includes the robust and battle-tested Facebook's Yoga as default layout engine.
  • Fine-grained recycling: Any component such as a text or image can be recycled and reused anywhere in the UI.

TL;DR

Let's build the classic Counter-Example.

The DSL to define the vdom representation is similiar to SwiftUI.

func makeCounterBodyFragment(context: Context, coordinator: CounterCoordinator) -> OpaqueNodeBuilder {
  Component<CounterCoordinator>(context: context) { context, coordinator in
    VStackNode {
      LabelNode(text: "\(coordinator.count)")
        .textColor(.darkText)
        .background(.secondarySystemBackground)
        .width(Const.size + 8 * CGFloat(coordinator.count))
        .height(Const.size)
        .margin(Const.margin)
        .cornerRadius(Const.cornerRadius)
      HStackNode {
        ButtonNode()
          .text("TAP HERE TO INCREASE COUNT")
          .setTarget(coordinator, action: #selector(CounterCoordinator.increase), for: .touchUpInside)
          .background(.systemTeal)
          .padding(Const.margin * 2)
          .cornerRadius(Const.cornerRadius)
      }
    }
    .alignItems(.center)
    .matchHostingViewWidth(withMargin: 0)
  }
}

screen

Label and Button are just specialized versions of the Node<V: UIView> pure function. That means you could wrap any UIView subclass in a vdom node. e.g.

Node(UIScrollView.self) {
  Node(UILabel.self).withLayoutSpec { spec in 
    // This is where you can have all sort of custom view configuration.
  }
  Node(UISwitch.self)
}

The withLayoutSpec modifier allows to specify a custom configuration closure for your view.

Coordinators are the only non-transient objects in CoreRender. They yeld the view internal state and they are able to manually access to the concrete view hierarchy (if one desires to do so).

By calling setNeedsReconcile the vdom is being recomputed and reconciled against the concrete view hiearchy.

class CounterCoordinator: Coordinator{
  var count: UInt = 0

  func incrementCounter() {
    self.count += 1                      // Update the state.
    setNeedsReconcile()                  // Trigger the reconciliation algorithm on the view hiearchy associated to this coordinator.
  }
}

Finally, Components are yet again transient value types that bind together a body fragment with a given coordinator.

class CounterViewCoordinator: UIViewController {
  var hostingView: HostingView!
  let context = Context()

  override func loadView() {
    hostingView = HostingView(context: context, with: [.useSafeAreaInsets]) { context in
      makeCounterBodyFragment(context: context, coordinator: coordinator)
    }
    self.view = hostingView
  }
    
  override func viewDidLayoutSubviews() {
    hostingView.setNeedsLayout()
  }
}

Components can be nested in the node hierarchy.

func makeFragment(context: Context) {
  Component<FooCoordinator>(context: context) { context, coordinator in
    VStackNode {
      LabelNode(text: "Foo")
      Component<BarCoordinator>(context: context) { context, coordinator in
        HStackNode {
          LabelNode(text: "Bar")
          LabelNode(text: "Baz")
        }
      }
    }
  }
}

Use it with SwiftUI

Render nodes can be nested inside SwiftUI bodies by using CoreRenderBridgeView:

struct ContentView: View {
  var body: some View {
    VStack {
      Text("Hello From SwiftUI")
      CoreRenderBridgeView { context in
        VStackNode {
          LabelNode(text: "Hello")
          LabelNode(text: "From")
          LabelNode(text: "CoreRender")
        }
          .alignItems(.center)
          .background(UIColor.systemGroupedBackground)
          .matchHostingViewWidth(withMargin: 0)
      }
      Text("Back to SwiftUI")
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

Credits:

Layout engine:

More Repositories

1

ios-fontawesome

NSString+FontAwesome
Objective-C
1,751
star
2

ios-realtimeblur

Multipurpose real-time blur view for iOS6+
Objective-C
956
star
3

Store

Unidirectional, transactional, operation-based Store implementation.
Swift
502
star
4

flexboxobjc_deprecated

A simple UIKit extension to wrap Flexbox layouts.
Objective-C
398
star
5

Buffer

Swift μ-framework for efficient array diffs and datasource adapters.
Swift
349
star
6

flexboxswfit_deprecated

(Deprecated) Port of Facebook's css-layout to Swift
Swift
307
star
7

DataStructures

A collection of Data Structures implemented in Swift.
Swift
48
star
8

Primer

Assign/Partial/ReadOnly/Proxy/Locks in Swift
Swift
32
star
9

CoreRender

Moved to https://github.com/alexdrone/Render
Objective-C++
26
star
10

S.swift

Get strong typed, autocompleted resources, color swatches and font styles in Swift projects from a simple YAML stylesheet.
Swift
24
star
11

YAS

Yet Another Stylesheet (YAML-based Stylesheet Engine)
Swift
23
star
12

Surface

A set of utilities and extensions to create depth in your views by using complex shadows.
Swift
18
star
13

PushID

Robust and thread-safe generator for uuids with guaranteed chronological ordering in Swift.
Swift
16
star
14

Emit

Event propagation and object observation library (Pre-Combine.framework).
Swift
10
star
15

ObjCTrait

Trait support for Objective-C
Objective-C
9
star
16

PatchComposer

A Quartz-composer like UI for patch editing in SwiftUI
Swift
8
star
17

SwiftUI_UIKit_Bridge

Share custom typography and color swatches between UIKit and SwiftUI.
Swift
6
star
18

Pbtxt

Schemaless protobuf text-format parser (with Codable support).
Swift
5
star
19

itu_pku_eventregexp

Java
5
star
20

DesignSystemTemplate

A customizable design system in Swift.
Swift
4
star
21

GuruMeditation

Nostalgic error screen for iOS.
Swift
4
star
22

Multipeer

Forked from: https://github.com/insidegui/MultipeerKit
Swift
3
star
23

ui_dsl_deprecated

(Deprecated) A lightweight native stylesheet engine for iOS
Objective-C
3
star
24

cxx_tutorials

A bunch of tutorials used to teach modern C++ and OpenGL to iOS devs
C++
3
star
25

Multi.kt

Kotlin Multiplatform Lib Template
Kotlin
3
star
26

swift1_rune_game

An iOS game inspired by 'Dots' based on the nordic runes
Objective-C
2
star
27

firebase_utils_deprecated

Firebase-backed store for Dispatch
Swift
2
star
28

trn_modelmapper

A ORM inspired by Ruby's DataMapper and ActiveRecord
Java
2
star
29

yaHN-android

Yet another HackerNews Android client
Java
2
star
30

itu_algorithms

Some algorithms back from Uni time
Ruby
2
star
31

FontCustom

fontcustom template for iOS
Objective-C
2
star
32

objc_latte_uikit_deprecated

(deprecated) An iOS markup language, framework and tool to help the design and the implementation of the presentation layer.
1
star