UIViewKit

1.8.0

UIViewKit is a Swift tool for easy UIKit view design, blending InterfaceBuilder's ease with Swift type checks. It mirrors SwiftUI's look and provides quick methods for attributes, outlets, and constraints. The @resultBuilder makes code faster to write and visually appealing.
Adobels/UIViewKit

What's New

1.8.0

2024-02-16T17:11:54Z

What's Changed

  • Split DSL and Kit API to separate Targets by @Adobels in #10

Enhencements:

DSL:

  • Remove auto-naming of constraints generated in iBAttributes.
  • Add support for the ShouldBeArchived property of NSLayoutConstraint.

Kit:

  • Add a UIViewController extension ibSetView to replace the view of the controller.
  • Add UIViewController extensions ibEmbed / ibUnembed to add view controllers and their views to container views.
  • Rename UIViewDebug to IBDebug.
  • IBDebug.showFrames receives default values for includeGivenView and includeUIKitPrivateViews, which are used rarely.

Full Changelog: 1.7.0...1.8.0

UIViewKit

UIViewKit is a Swift tool that makes designing and setting up UIKit views as simple as using InterfaceBuilder, but with Swift's strong type checks. It offers a look similar to SwiftUI and has lots of easy methods for attributes, outlets, and constraints. Thanks to the @resultBuilder attribute, the code is quick to write, looks cleaner, and is more pleasing to the eye.

Key Features

  • SwiftUI-Style Syntax for UIKit: Embrace SwiftUI's declarative approach, but tailored for UIKit.
  • No More Storyboards/Xibs: Design UI directly in code, bypassing storyboards and xib files.
  • Constraint Configuration Generator: Produce complex AutoLayout setups with a single method.
  • Previews for Views & Controllers: Preview your UIKit views and controllers in code, just like SwiftUI views.

How to Use

Defining ViewController's View, with "Hello, world!" label

class ViewController: UIViewController {

    var label: UILabel!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view {
            UILabel().ibOutlet(&label).ibAttributes {
                $0.centerXAnchor.constraint(equalTo: view.centerXAnchor)
                $0.centerYAnchor.constraint(equalTo: view.centerYAnchor)
                
                $0.text = "Hello, world!"
            }
        }
    }
}

Defining ViewController's View, Complex

class ViewControllerComplex: UIViewController {
    
    var labelTitle: UILabel!
    var labelsText: [UILabel] = []
    var button: UIButton = .init()
    var heightConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view {
            UIStackView(axis: .vertical, spacing: 10, alignment: .center) { stackView in
                UILabel().ibOutlet(&labelTitle).ibAttributes {
                    $0.text = "Title"
                    $0.font = .init(name: "Arial", size: 30)
                }
                UIView() { superview in
                    UILabel().ibOutlet(in: &labelsText).ibAttributes {
                        $0.topAnchor.constraint(equalTo: superview.topAnchor)
                        $0.leftAnchor.constraint(equalTo: superview.leftAnchor)
                        $0.rightAnchor.constraint(equalTo: superview.rightAnchor)
                        $0.bottomAnchor.constraint(equalTo: superview.bottomAnchor)
                        
                        $0.text = "Label 1"
                        $0.textColor = .red
                    }
                }
                UILabel().ibOutlet(in: &labelsText).ibAttributes {
                    $0.text = "Label 2"
                }
                UIView().ibAttributes {
                    $0.leftAnchor.constraint(equalTo: stackView.leftAnchor)
                    $0.heightAnchor.constraint(equalToConstant: 50).ibPriority(.defaultHigh - 1).ibOutlet(&heightConstraint)
                    
                    $0.backgroundColor = .yellow
                }
                UILabel().ibAttributes {
                    $0.text = "Label 3 Without Outlet"
                }
                UIButton().ibOutlet(&button).ibAttributes {
                    $0.widthAnchor.constraint(equalToConstant: 300)
                    
                    $0.setTitle("Button", for: .normal)
                    $0.backgroundColor = .blue
                    
                    $0.addTarget(self, action: #selector(didTap), for: .touchUpInside)
                }
            }.ibAttributes {
                $0.centerXAnchor.constraint(equalTo: view.centerXAnchor)
                $0.centerYAnchor.constraint(equalTo: view.centerYAnchor)
            }
        }
        
        labelsText.forEach { $0.text = "toto" }
    }
    
    @objc func didTap() {
        print(#function)
    }
}

#if DEBUG

import SwiftUI

struct ViewControllerPreviews: PreviewProvider {
    static var previews: some View {
        IBRepresentableFreeFormViewController(ViewControllerComplex())
    }
}

#endif

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu May 02 2024 07:09:11 GMT-0900 (Hawaii-Aleutian Daylight Time)