Create view hierarchies declaratively.

Related tags

UI swift ios view hierarchy
Overview

Panda


Carthage Compatible CocoaPods Compatible Swift 5 Platform Build Status

Create view hierarchies declaratively.

Quick Look

view.pd.add(
    imageView.pd.image(logoImage),
    label.pd.text("Logo").textColor(.red).font(size: 20),
    button.pd.title("Go").action(buttonTapped)
)

It’s much shorter and concise than the imperative API, and easier to edit than storyboards.

Installation

Carthage

github "wordlessj/Panda" ~> 2.0

CocoaPods

pod 'Panda', '~> 2.0'

Usage

Basics

ALL settable properties and set methods in UIKit and Core Animation are available in pd extension, set prefixes are removed.

Views are configured and added to a superview as you see in Quick Look, you can also nest add() to create a more complex hierarchy, note that configuration by pd is optional.

view.pd.add(
    containerView.pd.clipsToBounds(true).add(
        view1.pd.backgroundColor(.red)
    ),
    view2
)

Other types that can be added by add*() methods are also supported, like UIGestureRecognizer and UILayoutGuide. Don’t put them all in one add() method, grouping is recommended.

view.pd.add(
    // view hierarchy
).add(
    layoutGuide.pd.identifier("center guide")
).add(
    tapGestureRecognizer.pd.numberOfTapsRequired(2).action(doubleTapped)
)

Objects can be configured inline and passed to a method, if the method accepts a *Convertible.

// mask() accepts a UIViewConvertible.
// You can pass a plain or configured UIView.
view.pd.mask(
    maskView.pd.backgroundColor(.white)
)

Add and Remove

add() and remove() methods are available on types below, followed by types that can be added. remove() is basically used to remove multiple object at once without configuration.

Type Types which can be added
CALayer CALayer, CAAnimation
UIAlertController UIAlertAction, UIViewController, UIKeyCommand
UIDynamicAnimator UIDynamicBehavior
UIDynamicBehavior UIDynamicBehavior
UIView UIView, UIGestureRecognizer, UIMotionEffect, NSLayoutConstraint, UILayoutGuide, UIInteraction
UIViewController UIViewController, UIKeyCommand

UIView is added to UIStackView using addArrangedSubview().

Custom Properties

With smart key paths, you can use set() to set custom properties not found in Panda.

customView.pd.set(\.flashes, true)

Action

Target-actions are replaced with action() taking a closure. They’re available on following types.

  • CADisplayLink, an initializer instead of action().
  • UIAccessibilityCustomAction
  • UIBarButtonItem
  • UIControl
  • UIGestureRecognizer

Font

UIFont factory methods are available directly as following methods.

  • font(style:)
  • font(style:compatibleWith:)
  • font(size:)
  • font(size:weight:)
  • font(boldSize:)
  • font(italicSize:)
  • font(monospacedDigitSize:weight:)

They’re available on following types.

  • UIButton, set font of titleLabel.
  • UILabel
  • UISimpleTextPrintFormatter
  • UITextField
  • UITextView

Transform

There’re several convenient methods to set transform on UICollectionViewLayoutAttributes and UIView. Each of them has a corresponding concat*() method to concatenate a transform to existing transform.

  • rotation(radian:)
  • rotation(degree:)
  • scale(x:y:)
  • scale(_:)
  • translation(x:y:)

Control States and Bar Metrics

For set methods taking a UIControlState or UIBarMetrics like title(_:for:), there’re convenience methods to set for all states or metrics like title(_:highlighted:selected:disabled:), the first parameter is for normal state, and the other parameters are optional.

Compositions

For convenience, there’re several composition methods which set multiple properties at once.

Types Composition Components
CALayer border borderWidth, borderColor
CALayer shadow shadowOpacity, shadowRadius, shadowOffset, shadowColor, shadowPath
UICollectionView, UIPageViewController, UIPickerView, UITableView sourceDelegate dataSource, delegate
UILabel shadow shadowColor, shadowOffset
UIScrollView alwaysBounce alwaysBounceHorizontal, alwaysBounceVertical
UIScrollView showsScrollIndicators showsHorizontalScrollIndicator, showsVerticalScrollIndicator
UIView border layer.border
UIView cornerRadius layer.cornerRadius, layer.masksToBounds to true
UIView shouldRasterize layer.shouldRasterize, layer.rasterizationScale to screen's scale
UIView shadow layer.shadow

Set-Like Methods

Some methods don’t begin with set but are usually called while configuring, they are available as well.

  • UIAlertController.pd.addTextField(configure:)
  • UIGestureRecognizer.pd.require(toFail:)

Register and Dequeue

For UICollectionView and UITableView, an identifier is required when registering and dequeuing cells and views, and force casting to a specific type is usually needed. In practice, one identifier is associated with one specific type, so you can use the type itself instead of an identifier.

collectionView.pd.register(CustomCell.self)

let cell: CustomCell = collectionView.pd.dequeue(CustomCell.self, for: indexPath)

Reuse

If more than one object share similar configurations, or you want to create something like CSS, you can extract configurations into a method, then apply the method using do().

view.pd.add(
    firstLabel.pd.do(configLabel),
    secondLabel.pd.do(configLabel)
)

func configLabel(_ label: UILabel) {
    label.pd.textColor(.red).font(size: 20).numberOfLines(0)
}

Bamboo

Bamboo is a framework which makes Auto Layout and manual layout in one line, together with Panda, they make creating views in code incredibly simple and easy.

License

Panda is released under the MIT license. See LICENSE for details.

You might also like...
A simple, customizable view for efficiently collecting country information in iOS apps.
A simple, customizable view for efficiently collecting country information in iOS apps.

CountryPickerView CountryPickerView is a simple, customizable view for selecting countries in iOS apps. You can clone/download the repository and run

Elissa displays a notification on top of a UITabBarItem or any UIView anchor view to reveal additional information.
Elissa displays a notification on top of a UITabBarItem or any UIView anchor view to reveal additional information.

Elissa Attach a local notification to any UIView to reveal additional user guidance. Usage Example Per default, Elissa will try to align to the center

Cool Animated music indicator view written in Swift
Cool Animated music indicator view written in Swift

Cool Animated music indicator view written in Swift. ESTMusicIndicator is an implementation of NAKPlaybackIndicatorView in Swift for iOS 8. 本人著作的书籍《La

A child view controller framework that makes setting up your parent controllers as easy as pie.
A child view controller framework that makes setting up your parent controllers as easy as pie.

Description Family is a child view controller framework that makes setting up your parent controllers as easy as pie. With a simple yet powerful publi

An easy to use FAQ view for iOS written in Swift
An easy to use FAQ view for iOS written in Swift

FAQView An easy to use FAQ view for iOS written in Swift. This view is a subclass of UIView. Setup with CocoaPods If you are using CocoaPods add this

A container view that responds to scrolling of UIScrollView
A container view that responds to scrolling of UIScrollView

FlexibleHeader A container view that responds to scrolling of UIScrollView. normal threshold FlexibleHeaderExecutantType Getting Started Progressive I

A library, which adds the ability to hide navigation bar when view controller is pushed via hidesNavigationBarWhenPushed flag
A library, which adds the ability to hide navigation bar when view controller is pushed via hidesNavigationBarWhenPushed flag

HidesNavigationBarWhenPushed A library, which adds the ability to hide navigation bar when view controller is pushed via hidesNavigationBarWhenPushed

Swift based simple information view with pointed arrow.
Swift based simple information view with pointed arrow.

InfoView View to show small text information blocks with arrow pointed to another view.In most cases it will be a button that was pressed. Example To

An easy way to add a shimmering effect to any view with just one line of code. It is useful as an unobtrusive loading indicator.
An easy way to add a shimmering effect to any view with just one line of code. It is useful as an unobtrusive loading indicator.

LoadingShimmer An easy way to add a shimmering effect to any view with just single line of code. It is useful as an unobtrusive loading indicator. Thi

Comments
  • Use of unresolved identifier 'imageView'; did you mean 'UIImageView'?

    Use of unresolved identifier 'imageView'; did you mean 'UIImageView'?

    Unable to load Panda library using your sample code.

    Xcode complains: Use of unresolved identifier 'imageView'; did you mean 'UIImageView'?

    Code:

    import UIKit
    import Panda
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            view.pd.add(
                imageView.pd.image("testimage"),
                label.pd.text("Logo").textColor(.red).font(size: 20),
                button.pd.title("Go").action(buttonTapped)
            )
            
            // Do any additional setup after loading the view.
        }
    
    
    }
    opened by gokalper 1
Releases(2.0.0)
Owner
Javier Zhang
iOS developer, ❤️ UI design.
Javier Zhang
Full configurable spreadsheet view user interfaces for iOS applications. With this framework, you can easily create complex layouts like schedule, gantt chart or timetable as if you are using Excel.

kishikawakatsumi/SpreadsheetView has moved! It is being actively maintained at bannzai/SpreadsheetView. This fork was created when the project was mov

Kishikawa Katsumi 34 Sep 26, 2022
A way to quickly add a notification badge icon to any view. Make any view of a full-fledged animated notification center.

BadgeHub A way to quickly add a notification badge icon to any view. Demo/Example For demo: $ pod try BadgeHub To run the example project, clone the r

Jogendra 772 Dec 28, 2022
Create SwiftUI Views with any data

Create SwiftUI Views with any data

Zach Eriksen 20 Jun 27, 2022
Easily use UIKit views in your SwiftUI applications. Create Xcode Previews for UIView elements

SwiftUIKitView Easily use UIKit views in SwiftUI. Convert UIView to SwiftUI View Create Xcode Previews from UIView elements SwiftUI functional updatin

Antoine van der Lee 682 Dec 29, 2022
Create macOS apps with Swift packages instead of Xcode projects

Swift Bundler A Swift Package Manager wrapper that allows the creation of MacOS apps with Swift packages instead of Xcode projects. My motivation is t

null 182 Dec 25, 2022
Create descriptive UIKit screens, faster!

Columbina's DeclarativeUIKit Create descriptive UIKit screens, faster! Get rid of constraints manipulation and use declarative language to create your

Columbina 2 Dec 12, 2021
A UIControl subclass that makes it easy to create empty states.

AZEmptyState Making empty state simple. Screenshots Installation Cocoa Pods: pod 'AZEmptyState' Manual: Simply drag and drop the Sources folder to you

Antonio Zaitoun 88 Oct 2, 2022
NotSwiftUI is designed to help you create UI components quickly and efficiently with code!

NotSwiftUI NotSwiftUI is designed to help you create UI components quickly and efficiently with code! Capitalizing on the idea that most of the UI ele

Jonathan G. 50 Dec 20, 2022
A custom stretchable header view for UIScrollView or any its subclasses with UIActivityIndicatorView and iPhone X safe area support for content reloading. Built for iOS 10 and later.

Arale A custom stretchable header view for UIScrollView or any its subclasses with UIActivityIndicatorView support for reloading your content. Built f

Putra Z. 43 Feb 4, 2022
Play BreakOut while loading - A playable pull to refresh view using SpriteKit

BreakOutToRefresh Play BreakOut while loading - A playable pull to refresh view using SpriteKit BreakOutToRefresh uses SpriteKit to add a playable min

Dominik Hauser 2.5k Jan 5, 2023