Skip to content

Instantly share code, notes, and snippets.

@akisute
Created January 12, 2017 10:19
Show Gist options
  • Save akisute/6c3817f9672ce70e3dff9d8ca37a9d99 to your computer and use it in GitHub Desktop.
Save akisute/6c3817f9672ce70e3dff9d8ca37a9d99 to your computer and use it in GitHub Desktop.
Example layout code in playground
//: Playground - noun: a place where people can play
import Foundation
import UIKit
import PlaygroundSupport
// Needs this settings first before using UIWebView to render web pages in Playground
// Also required to run Auto Layout
PlaygroundPage.current.needsIndefiniteExecution = true
/**
Example 1: Auto Layout
In a production application, you need to setup constraints when views are created, like `viewDidLoad()` or `init()`
Make sure to set translatesAutoresizingMaskIntoConstraints to false immediately after you initialized subviews
*/
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
view.backgroundColor = UIColor.lightGray
PlaygroundPage.current.liveView = view
let menuBar = UIView()
menuBar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(menuBar)
menuBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
menuBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
menuBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
menuBar.backgroundColor = UIColor.yellow
let toolbar = UIView()
toolbar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(toolbar)
toolbar.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
toolbar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
toolbar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
toolbar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
toolbar.backgroundColor = UIColor.green
let webView = UIWebView()
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
webView.topAnchor.constraint(equalTo: menuBar.bottomAnchor, constant: 8.0).isActive = true
webView.bottomAnchor.constraint(equalTo: toolbar.topAnchor, constant: -8.0).isActive = true
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
webView.loadRequest(URLRequest(url: URL(string:"https://www.google.co.jp")!))
/**
Example 2: Manual Layout
In a production application, separate the initialization code of your subviews and layout code.
Initializations should be done in viewDidLoad(), init() or something like that, but the manual layout has to be done within
UIView.layoutSubviews() or UIViewController.viewDidLayoutSubviews().
You can actually mix auto layout with manual layout but it causes too much confuses especially for unexperienced developers. You should avoid it
unless you know exactly what you're doing.
*/
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
view.backgroundColor = UIColor.lightGray
PlaygroundPage.current.liveView = view
let menuBar = UIView()
view.addSubview(menuBar)
menuBar.frame = CGRect(x: view.frame.minX, y: view.frame.minY, width: view.frame.width, height: 64.0)
menuBar.backgroundColor = UIColor.yellow
let toolbar = UIView()
view.addSubview(toolbar)
toolbar.frame = CGRect(x: view.frame.minX, y: view.frame.maxY-64.0, width: view.frame.width, height: 64.0)
toolbar.backgroundColor = UIColor.green
let webView = UIWebView()
view.addSubview(webView)
webView.frame = CGRect(x: view.frame.minX,
y: menuBar.frame.maxY+8.0,
width: view.frame.width,
height: view.frame.height - menuBar.frame.height - toolbar.frame.height - 16.0)
webView.loadRequest(URLRequest(url: URL(string:"https://www.google.co.jp")!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment