Skip to content

The best way to use HStack and VStack in UIKit, and also supports Spacer and Divider. 在 UIKit 中使用 HStack 和 VStack 的绝佳方式,同时支持 Spacer 和 Divider。

License

Notifications You must be signed in to change notification settings

iWECon/StackKit

Repository files navigation

StackKit

☝️ A layout library similar to SwiftUI.

🔥 Support Combine (available iOS 13.0+)

⚠️ If you have any questions or new ideas, you can bring me a PR at any time.

Previews and codes

Preview 1

Demo

HStackView(alignment: .center, distribution: .spacing(14), padding: UIEdgeInsets(top: 14, left: 14, bottom: 14, right: 14)) {
    logoView.stack.size(80)
    VStackView(alignment: .left, distribution: .spacing(6)) {
        nameLabel
        briefLabel.stack.maxWidth(220)
    }
}

Demo

⚠️ If you have any question, please see the demo in Demo/Demo.xcodeproj or submit issue.

💡 There are no more examples, you can help to complete/optimize.

HStackView / VStackView

Alignment, Distribution, Padding and resultBuilder

HStackView(alignment: HStackAlignment, distribution: HStackDistribution, padding: UIEdgeInsets, content: @resultBuilder)
VStackView(alignment: VStackAlignment, distribution: VStackDistribution, padding: UIEdgeInsets, content: @resultBuilder)

Subview size is fixed

logoView.stack.size(CGSize?)

Min or Max width/height

briefLabel.stack.minWidth(CGFloat?)
                .maxWidth(CGFloat?)
                .minHeight(CGFloat?)
                .maxHeight(CGFloat?)

Offset

briefLabel.stack.offset(CGPoint?)
                .offset(x: CGFloat?)
                .offset(y: CGFloat?)

SizeToFit

briefLabel.stack.width(220).sizeToFit(.width) // see `UIView+FitSize.swift`

Spacer & Divider

VStackView {
    nameLabel
    
    Spacer(length: 2) //< see `Spacer.swift`
    Divider(thickness: 2) // see `Divider.swift`
    
    briefLabel
}

Then

VStackView {
    briefLabel.stack.then {
        $0.textColor = .red
    }
}

Change and update

// update text
briefLabel.text = "Bump version to 1.2.2"

// stackContainer means any instance of HStackView or VStackView
stackContainer.setNeedsLayout() // or .sizeToFit() 

Combine

import Combine

// ...
@Published var name: String = "StackKit"

var cancellables = Set<AnyCancellable>()

// ...
HStackView {
    UILabel()
        .stack
        .then {
            $0.font = .systemFont(ofSize: 16)
            $0.textColor = .systemPink
        }
        .receive(text: $name, storeIn: &cancellables)
}

// update name
self.name = "StackKit version 1.2.3"
// update stackView
stackView.setNeedsLayout()

// remember cleanup cancellables in deinit
deinit {
    // the effective same as `cancellables.forEach { $0.cancel() }`
    cancellables.removeAll()
}

🤔

I'm not very good at writing documents. If you have the advantages in this regard, please submit PR to me. Thank you very much.

Installation

.package(url: "https://github.com/iWECon/StackKit.git", from: "1.0.0")

About

The best way to use HStack and VStack in UIKit, and also supports Spacer and Divider. 在 UIKit 中使用 HStack 和 VStack 的绝佳方式,同时支持 Spacer 和 Divider。

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages