JWChainableUIView allows you to chain functions of initialization, addSubview, customization your UIView.
Making it simpler to create and layout subviews, avoiding messy frame calculations.
japaneseFlagView.initSubviewInCenter(classType: CircleView.self, size: CGSize(width: 50, height: 50))
.changeBackgroundColor(UIColor.redColor())
.changeAlpha(0.5)
mickyMouseView.initSubviewInBottomCenter(classType: CircleView.self, size: CGSize(width: 80, height: 80))
.changeBackgroundColor(UIColor.blackColor())
.offSet(x: 0, y: -10)
mickyMouseView.initSubviewInTopLeft(classType: CircleView.self, size: CGSize(width: 35, height: 35))
.changeBackgroundColor(UIColor.blackColor())
.offSet(x: 10, y: 15)
mickyMouseView.initSubviewInTopRight(classType: CircleView.self, size: CGSize(width: 35, height: 35))
.changeBackgroundColor(UIColor.blackColor())
.offSet(x: -10, y: 15)
let stripeHeight: CGFloat = 1000.0 / 13
for index in 0..<13 {
let redStripe = americanFlagView.initSubview(classType: UIView.self, size: CGSize(width: 1500, height: stripeHeight))
.offSet(x: 0, y: CGFloat(index) * stripeHeight)
if index % 2 == 0 {
redStripe.changeBackgroundColor(UIColor.redColor())
}
}
let bluePart = americanFlagView.initSubviewInTopLeft(classType: UIView.self, size: CGSize(width: 500, height: 500))
.changeBackgroundColor(UIColor.blueColor())
for index in 0..<49 {
let y = CGFloat(index / 7) * 500.0 / 7
let x = CGFloat(index % 7) * 500.0 / 7
bluePart.initSubview(classType: StarView.self, size: CGSize(width: 50, height: 50))
.offSet(x: x, y: y)
}
##Usage :
Chaining:
view.initSubview(classType: UIView.self, size: CGSize(width: 50, height: 50))
.changeAlpha(0.5)
.changeBackgroundColor(UIColor.blackColor())
.align(alignment: .Right, .Bottom)
.offSet(x: 15, y: 15)
Alignment:
public enum HorizontalAlignment {
case Left, Center, Right
}
public enum VerticalAlignment {
case Top, Center, Bottom
}
//Align view in the top left corner of superview
view.align(alignment: .Left, .Top)
Moving:
view.moveToCenter()
view.moveToTopLeft()
view.moveToTopRight()
view.moveToTopCenter()
view.moveToBottomLeft()
view.moveToBottomRight()
view.moveToBottomCenter()
view.moveToLeftCenter()
view.moveToRightCenter()
Scaling:
//Scales the view based on its center.
view.scale(2.0)
Convenience initializers make it even simpler to initialize your subview exactly where you want in one line.
view.initSubviewInCenter(classType: CircleView.self, size: size)
view.initSubviewInTopLeft(classType: CircleView.self, size: size)
view.initSubviewInTopRight(classType: CircleView.self, size: size)
view.initSubviewInTopCenter(classType: CircleView.self, size: size)
view.initSubviewInBottomLeft(classType: CircleView.self, size: size)
view.initSubviewInBottomRight(classType: CircleView.self, size: size)
view.initSubviewInBottomCenter(classType: CircleView.self, size: size)
view.initSubviewInLeftCenter(classType: CircleView.self, size: size)
view.initSubviewInRightCenter(classType: CircleView.self, size: size)
##Installation
- Download the project.
- Open the "JWChainableUIView.playground" if you want to play around and experiment with the methods. All examples above are included in the playground.
- Copy the "JWChainableUIViewExtension.swift" from the "Sources" folder and add it to the project you want to use it in.