Skip to content

Commit

Permalink
Merged the example project
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanzummeren committed Sep 5, 2015
1 parent 8054004 commit 462138b
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 127 deletions.
23 changes: 12 additions & 11 deletions Tests/TZStackView/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@

//
// AppDelegate.swift
// TZStackView
// TZStackView-Example
//
// Created by Tom van Zummeren on 10/06/15.
// Created by Tom van Zummeren on 20/06/15.
// Copyright (c) 2015 Tom van Zummeren. All rights reserved.
//


import UIKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Appearance proxy for the UISegmentedControl font
UISegmentedControl.appearance().setTitleTextAttributes(
[NSFontAttributeName:UIFont(name: "HelveticaNeue-Light", size:10.0)!],
forState: UIControlState.Normal)

if #available(iOS 9.0, *) {
window?.rootViewController = ViewController()
} else {
window?.rootViewController = UIViewController()
}
//Launch the application
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
}

38 changes: 6 additions & 32 deletions Tests/TZStackView/ExplicitIntrinsicContentSizeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,32 @@ class ExplicitIntrinsicContentSizeView: UIView {

let name: String
let contentSize: CGSize

let baselineView: UIView = UIView()

var baselinePercentage: CGFloat
var baselineHeightPercentage: CGFloat

init(intrinsicContentSize: CGSize, name: String, baselinePercentage: CGFloat, baselineHeightPercentage: CGFloat) {
self.baselinePercentage = baselinePercentage
self.baselineHeightPercentage = baselineHeightPercentage
init(intrinsicContentSize: CGSize, name: String) {
self.name = name
self.contentSize = intrinsicContentSize
super.init(frame: CGRectZero)

let gestureRecognizer = UITapGestureRecognizer(target: self, action: "tap")
addGestureRecognizer(gestureRecognizer)
userInteractionEnabled = true

baselineView.backgroundColor = UIColor(white: 1, alpha: 0.5)
addSubview(baselineView)
}

func tap() {
UIView.animateWithDuration(0.5, delay:0, options: .AllowUserInteraction, animations: {
UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: .AllowUserInteraction, animations: {
self.hidden = true
}, completion: nil)
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
super.layoutSubviews()

let baselineY = bounds.size.height * baselinePercentage
let baselineHeight = bounds.size.height * baselineHeightPercentage
baselineView.frame = CGRect(x: bounds.size.width * (1 / 10), y: baselineY, width: bounds.size.width * (8 / 10), height: baselineHeight)
}

override func viewForBaselineLayout() -> UIView {
return baselineView
}

override var viewForLastBaselineLayout: UIView {
return baselineView
}

override func intrinsicContentSize() -> CGSize {
return contentSize
}
override var description: String {

override var description: String {
return name
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/TZStackView/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<array>
<string>armv7</string>
</array>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down
226 changes: 142 additions & 84 deletions Tests/TZStackView/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,120 +1,178 @@
//
// ViewController.swift
// TZStackView
// TZStackView-Example
//
// Created by Tom van Zummeren on 10/06/15.
// Created by Tom van Zummeren on 20/06/15.
// Copyright (c) 2015 Tom van Zummeren. All rights reserved.
//

import UIKit

@available(iOS 9.0, *)
class ViewController: UIViewController {

var stackView: UIStackView!

//MARK: - Properties
//--------------------------------------------------------------------------
var tzStackView: TZStackView!

let resetButton = UIButton(type: .System)
let instructionLabel = UILabel()

var axisSegmentedControl: UISegmentedControl!
var alignmentSegmentedControl: UISegmentedControl!
var distributionSegmentedControl: UISegmentedControl!

let debugButton = UIButton(type: .System)

//MARK: - Lifecyle
//--------------------------------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()

// Configure original UIStackView
stackView = UIStackView(arrangedSubviews: createViews())
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .Horizontal
stackView.distribution = .FillEqually
stackView.alignment = .Center
stackView.baselineRelativeArrangement = true
stackView.spacing = 15
stackView.layoutMarginsRelativeArrangement = true
view.addSubview(stackView)

// Configure backwards compatible TZStackView the same way as UIStackView above
edgesForExtendedLayout = .None;

view.backgroundColor = UIColor.blackColor()
title = "TZStackView"

tzStackView = TZStackView(arrangedSubviews: createViews())
tzStackView.translatesAutoresizingMaskIntoConstraints = false
tzStackView.axis = .Horizontal
tzStackView.distribution = .FillEqually
tzStackView.alignment = .Center
tzStackView.axis = .Vertical
tzStackView.distribution = .Fill
tzStackView.alignment = .Fill
tzStackView.spacing = 15
tzStackView.layoutMarginsRelativeArrangement = true
view.addSubview(tzStackView)


instructionLabel.translatesAutoresizingMaskIntoConstraints = false
instructionLabel.font = UIFont.systemFontOfSize(15)
instructionLabel.text = "Tap any of the boxes to set hidden=true"
instructionLabel.textColor = UIColor.whiteColor()
instructionLabel.numberOfLines = 0
instructionLabel.setContentCompressionResistancePriority(900, forAxis: .Horizontal)
view.addSubview(instructionLabel)

resetButton.translatesAutoresizingMaskIntoConstraints = false
resetButton.setTitle("reset", forState: .Normal)
resetButton.setTitle("Reset", forState: .Normal)
resetButton.addTarget(self, action: "reset", forControlEvents: .TouchUpInside)
resetButton.setContentCompressionResistancePriority(1000, forAxis: .Horizontal)
resetButton.setContentHuggingPriority(1000, forAxis: .Horizontal)
view.addSubview(resetButton)

debugButton.translatesAutoresizingMaskIntoConstraints = false
debugButton.setTitle("debug", forState: .Normal)
debugButton.addTarget(self, action: "debugPrint", forControlEvents: .TouchUpInside)
view.addSubview(debugButton)
axisSegmentedControl = UISegmentedControl(items: ["Vertical", "Horizontal"])
axisSegmentedControl.selectedSegmentIndex = 0
axisSegmentedControl.addTarget(self, action: "axisChanged:", forControlEvents: .ValueChanged)
axisSegmentedControl.setContentCompressionResistancePriority(900, forAxis: .Horizontal)
axisSegmentedControl.tintColor = UIColor.lightGrayColor()

alignmentSegmentedControl = UISegmentedControl(items: ["Fill", "Center", "Leading", "Top", "Trailing", "Bottom", "FirstBaseline"])
alignmentSegmentedControl.selectedSegmentIndex = 0
alignmentSegmentedControl.addTarget(self, action: "alignmentChanged:", forControlEvents: .ValueChanged)
alignmentSegmentedControl.setContentCompressionResistancePriority(1000, forAxis: .Horizontal)
alignmentSegmentedControl.tintColor = UIColor.lightGrayColor()

distributionSegmentedControl = UISegmentedControl(items: ["Fill", "FillEqually", "FillProportionally", "EqualSpacing", "EqualCentering"])
distributionSegmentedControl.selectedSegmentIndex = 0
distributionSegmentedControl.addTarget(self, action: "distributionChanged:", forControlEvents: .ValueChanged)
distributionSegmentedControl.tintColor = UIColor.lightGrayColor()

let views:[String: AnyObject] = ["stackView": stackView, "tzStackView": tzStackView, "resetButton": resetButton, "debugButton": debugButton]

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[debugButton]-[resetButton]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[resetButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[debugButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
let controlsLayoutContainer = TZStackView(arrangedSubviews: [axisSegmentedControl, alignmentSegmentedControl, distributionSegmentedControl])
controlsLayoutContainer.axis = .Vertical
controlsLayoutContainer.translatesAutoresizingMaskIntoConstraints = false
controlsLayoutContainer.spacing = 5
view.addSubview(controlsLayoutContainer)

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[stackView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[tzStackView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[tzStackView(==stackView)]-1-[stackView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
let views: [String:AnyObject] = [
"instructionLabel": instructionLabel,
"resetButton": resetButton,
"tzStackView": tzStackView,
"controlsLayoutContainer": controlsLayoutContainer
]

let metrics: [String:AnyObject] = [
"gap": 10,
"topspacing": 25
]

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-gap-[instructionLabel]-[resetButton]-gap-|",
options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[tzStackView]|",
options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[controlsLayoutContainer]|",
options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views))

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-topspacing-[instructionLabel]-gap-[controlsLayoutContainer]-gap-[tzStackView]-(>=0)-|",
options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-topspacing-[resetButton]-gap-[controlsLayoutContainer]",
options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views))
}

private func createViews() -> [UIView] {
let redView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 100, height: 100), name: "Red")
let greenView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 80, height: 80), name: "Green")
let blueView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 60, height: 60), name: "Blue")
let purpleView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 80, height: 80), name: "Purple")
let yellowView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 100, height: 100), name: "Yellow")

redView.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.75)
greenView.backgroundColor = UIColor.greenColor().colorWithAlphaComponent(0.75)
blueView.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.75)
purpleView.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.75)
yellowView.backgroundColor = UIColor.yellowColor().colorWithAlphaComponent(0.75)
return [redView, greenView, blueView, purpleView, yellowView]
}


//MARK: - Button Actions
//--------------------------------------------------------------------------
func reset() {
UIView.animateWithDuration(0.5) {
for view in self.stackView.arrangedSubviews {
view.hidden = false
}
UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: .AllowUserInteraction, animations: {
for view in self.tzStackView.arrangedSubviews {
view.hidden = false
}
}, completion: nil)

}

//MARK: - Segmented Control Actions
//--------------------------------------------------------------------------
func axisChanged(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
tzStackView.axis = .Vertical
default:
tzStackView.axis = .Horizontal
}
}

private func createViews() -> [UIView] {
let redView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 80, height: 80), name: "Red", baselinePercentage: 0, baselineHeightPercentage: 0.2)
let greenView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 80, height: 80), name: "Green", baselinePercentage: 0.4, baselineHeightPercentage: 0.3)
let blueView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 80, height: 80), name: "Blue", baselinePercentage: 0.2, baselineHeightPercentage: 0.4)
let purpleView = ExplicitIntrinsicContentSizeView(intrinsicContentSize: CGSize(width: 80, height: 80), name: "Purple", baselinePercentage: 0.6, baselineHeightPercentage: 0.1)

redView.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.75)
greenView.backgroundColor = UIColor.greenColor().colorWithAlphaComponent(0.75)
blueView.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.75)
purpleView.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.75)
return [redView, greenView, blueView, purpleView]
func alignmentChanged(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
tzStackView.alignment = .Fill
case 1:
tzStackView.alignment = .Center
case 2:
tzStackView.alignment = .Leading
case 3:
tzStackView.alignment = .Top
case 4:
tzStackView.alignment = .Trailing
case 5:
tzStackView.alignment = .Bottom
default:
tzStackView.alignment = .FirstBaseline
}
tzStackView.setNeedsUpdateConstraints()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

debugPrint()
func distributionChanged(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
tzStackView.distribution = .Fill
case 1:
tzStackView.distribution = .FillEqually
case 2:
tzStackView.distribution = .FillProportionally
case 3:
tzStackView.distribution = .EqualSpacing
default:
tzStackView.distribution = .EqualCentering
}
}

func debugPrint() {
// print("== UIStackView (\(stackView.constraints.count) constraints) ==")
// for constraint in stackView.constraints {
// print(constraint.readableString())
// }
// for subview in stackView.arrangedSubviews {
// print("\(subview):")
// for constraint in subview.constraints {
// print(" \(constraint.readableString())")
// }
// }
// print("")
// print("== TZStackView (\(tzStackView.constraints.count) constraints) ==")
// for constraint in tzStackView.constraints {
// print(constraint.readableString())
// }
// for subview in tzStackView.arrangedSubviews {
// print("\(subview):")
// for constraint in subview.constraints {
// print(" \(constraint.readableString())")
// }
// }

override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
}

0 comments on commit 462138b

Please sign in to comment.