QLKit brings the declarative power and developer experience of SwiftUI to UIKit. Built on top of QuickLayout, it allows you to build complex UI layouts using a syntax you already love, while keeping the full power and flexibility of UIKit components.
- 🏗 Declarative Syntax: Write UIKit layouts using
VStack,HStack,ZStackand more, just like SwiftUI. - 🔌 Seamless UIKit Integration: Use standard
UIView,UILabel,UIButtondirectly in your declarative stacks. - 📜 Easy Scrolling:
QLScrollViewmakes creating scrollable content trivial. - 🚀 Hosting Controllers:
QLHostingControllerandQLComposableHostingControllerbridge the gap between declarative layouts and view controller lifecycles. - 🧩 Component-Oriented: Encourage breaking down UI into smaller, reusable components.
- iOS 15.0+
- Swift 5.5+
Add QLKit to your Package.swift dependencies:
dependencies: [
.package(url: "https://github.com/astralchen/QLKit.git", branch: "main")
]Subclass QLHostingController to define your UI declaratively.
import UIKit
import QLKit
import QuickLayout
class MyViewController: QLHostingController {
let titleLabel = UILabel()
let subtitleLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = "Hello QLKit"
titleLabel.font = .boldSystemFont(ofSize: 24)
subtitleLabel.text = "SwiftUI-like syntax for UIKit"
subtitleLabel.textColor = .gray
}
// Define layout declaratively
override var body: Layout {
VStack(alignment: .center, spacing: 10) {
titleLabel
subtitleLabel
}
.padding(.all, 20)
}
}For simple screens, avoid subclassing by using QLComposableHostingController.
let titleLabel = UILabel()
titleLabel.text = "Quick Setup"
let vc = QLComposableHostingController {
VStack {
titleLabel
}
.center()
}
.withTitle("Home")
.withBackgroundColor(.white)ScrollView automatically manages content size and layout for you.
class MyViewController: QLHostingController {
let scrollView = QLScrollView()
override var body: Layout {
// Add content declaratively
ScrollView(scrollView) {
headerView
contentLabel
footerView
}
}
}Combine horizontal and vertical stacks to create complex dashboards.
override var body: Layout {
VStack(spacing: 24) {
// Header
HStack(spacing: 12) {
imageView
.resizable()
.frame(width: 50, height: 50)
VStack(alignment: .leading) {
nameLabel
statusLabel
}
Spacer()
}
.padding(.all, 16)
.background {
// Return a UIView for background
let bg = UIView()
bg.backgroundColor = .secondarySystemBackground
bg.layer.cornerRadius = 12
return bg
}
// Content
Spacer()
}
}QLKit is released under the MIT License. See LICENSE for details.