Skip to content

Instantly share code, notes, and snippets.

@gillygize
Created November 20, 2017 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gillygize/1db440bbb4102b23115e14ca293fc4e5 to your computer and use it in GitHub Desktop.
Save gillygize/1db440bbb4102b23115e14ca293fc4e5 to your computer and use it in GitHub Desktop.
manual-layout-revisited-playground
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
lazy var view1: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
lazy var view2: UIView = {
let view = UIView()
view.backgroundColor = .blue
return view
}()
lazy var view3: UIView = {
let view = UIView()
view.backgroundColor = .green
return view
}()
lazy var view4: UIView = {
let view = UIView()
view.backgroundColor = .yellow
return view
}()
override func loadView() {
let view = UIView(
frame: CGRect(
x: 0,
y: 0,
width: 320,
height: 480
)
)
view.backgroundColor = .white
view.layer.borderWidth = 1.0
view.layer.borderColor = UIColor.black.cgColor
[ view1, view2, view3, view4 ].forEach { view.addSubview($0) }
self.view = view
}
}
extension UIView {
var width: CGFloat {
get {
return frame.width
}
set(newValue) {
frame.size.width = newValue
}
}
var height: CGFloat {
get {
return frame.height
}
set(newValue) {
frame.size.height = newValue
}
}
var bottom: CGFloat {
get {
return frame.maxY
}
set(newValue) {
frame.origin.y = newValue - frame.height
}
}
var top: CGFloat {
get {
return frame.minY
}
set(newValue) {
frame.origin.y = newValue
}
}
var left: CGFloat {
get {
return frame.minX
}
set(newValue) {
frame.origin.x = newValue
}
}
var bottomRightCorner: CGPoint {
get {
return CGPoint(x: frame.maxX, y: frame.maxY)
}
set(newValue) {
frame.origin = CGPoint(
x: newValue.x - frame.size.width,
y: newValue.y - frame.size.height
)
}
}
}
func resize(views: [UIView], withWidth width: CGFloat) {
views.forEach { view in
view.width = width
}
}
func resize(views: [UIView], withHeight height: CGFloat) {
views.forEach { view in
view.height = height
}
}
func reposition(views: [UIView], withTop top: CGFloat) {
views.forEach { view in
view.top = top
}
}
func reposition(views: [UIView], withLeft left: CGFloat) {
views.forEach { view in
view.left = left
}
}
func arrangeTopToBottom(views: [UIView]) {
guard var currentPosition = views.first?.top else { return }
for view in views {
view.top = currentPosition
currentPosition += view.height
}
}
func resize(view: UIView, in views: [UIView], toFitHeight height: CGFloat) {
let heightOfViews = views.reduce(CGFloat(0)) { memo, currentView in
guard view != currentView else { return memo }
var mutableMemo = memo
mutableMemo += view.height
return mutableMemo
}
view.height = height - heightOfViews
}
func resize(views:[UIView], toFitHeight height: CGFloat) {
views.forEach { view in
view.height = height / CGFloat(views.count)
}
}
let viewController = MyViewController()
/// --- Demo 1
//viewController.view1.height = 100
//viewController.view1.width = 100
//viewController.view1.bottomRightCorner = viewController.view.bottomRightCorner
/// -- Demo 2
//let views = [
// viewController.view1,
// viewController.view2,
// viewController.view3,
// viewController.view4
//]
//
//resize(views: views, withWidth: viewController.view.width)
//resize(views: views, withHeight: 50)
/// -- Demo 3
//let views = [
// viewController.view1,
// viewController.view2,
// viewController.view3,
// viewController.view4
//]
//
//resize(views: views, withWidth: viewController.view.width)
//resize(views: views, withHeight: 50)
//arrangeTopToBottom(views: views)
// -- Demo 4
//let views = [
// viewController.view1,
// viewController.view2,
// viewController.view3,
// viewController.view4
//]
//
//resize(views: views, withWidth: viewController.view.width)
//resize(views: views, withHeight: 100)
//resize(view: viewController.view3, in: views, toFitHeight: viewController.view.height)
//arrangeTopToBottom(views: views)
// -- Demo 5
//let views = [
// viewController.view1,
// viewController.view2,
// viewController.view3,
// viewController.view4
//]
//
//resize(views: views, withWidth: viewController.view.width)
//resize(views: views, withHeight: 100)
//resize(views: views, toFitHeight: viewController.view.height)
//arrangeTopToBottom(views: views)
PlaygroundPage.current.liveView = viewController.view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment