Home

Awesome

Panda

<br>

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.

TypeTypes which can be added
CALayerCALayer, CAAnimation
UIAlertControllerUIAlertAction, UIViewController, UIKeyCommand
UIDynamicAnimatorUIDynamicBehavior
UIDynamicBehaviorUIDynamicBehavior
UIViewUIView, UIGestureRecognizer, UIMotionEffect, NSLayoutConstraint, UILayoutGuide, UIInteraction
UIViewControllerUIViewController, 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.

Font

UIFont factory methods are available directly as following methods.

They’re available on following types.

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.

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.

TypesCompositionComponents
CALayerborderborderWidth, borderColor
CALayershadowshadowOpacity, shadowRadius, shadowOffset, shadowColor, shadowPath
UICollectionView, UIPageViewController, UIPickerView, UITableViewsourceDelegatedataSource, delegate
UILabelshadowshadowColor, shadowOffset
UIScrollViewalwaysBouncealwaysBounceHorizontal, alwaysBounceVertical
UIScrollViewshowsScrollIndicatorsshowsHorizontalScrollIndicator, showsVerticalScrollIndicator
UIViewborderlayer.border
UIViewcornerRadiuslayer.cornerRadius, layer.masksToBounds to true
UIViewshouldRasterizelayer.shouldRasterize, layer.rasterizationScale to screen's scale
UIViewshadowlayer.shadow

Set-Like Methods

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

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.