Skip to content

Instantly share code, notes, and snippets.

@erica
Created January 26, 2016 18:04
Show Gist options
  • Save erica/4fa60524d9b71bfa9819 to your computer and use it in GitHub Desktop.
Save erica/4fa60524d9b71bfa9819 to your computer and use it in GitHub Desktop.
import UIKit
import XCPlayground
class ViewController: UIViewController {
func action() { print("Bing!") }
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .whiteColor()
let mySwitch : UISwitch = {
view.addSubview($0)
CenterViewInSuperview($0,
horizontal: true, vertical: true)
$0.addTarget(self, action: "action",
forControlEvents: .TouchUpInside)
return $0
}(UISwitch())
let _ : UILabel = {
view.addSubview($0)
CenterViewInSuperview($0,
horizontal: true, vertical: false)
$0.text = "Toggle me"
$0.font = UIFont.boldSystemFontOfSize(36)
ConstrainViews("V:[view1]-30-[view2]",
views: $0, mySwitch)
return $0
}(UILabel())
}
}
ViewController()
XCPlaygroundPage.currentPage.liveView = ViewController()
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
@dirk68-fu
Copy link

unfortunately CenterViewInSuperview is missing -> Use of unresolved identifier "CenterViewInSuperview"

@muescha
Copy link

muescha commented Nov 24, 2016

you can use this tiny library for CenterViewInSuperview:
https://gist.github.com/erica/a0f4ee23e9ee5dc1b489

@qiushuitian
Copy link

然而,用这种方式在XCode10.2.1上无法使用代码提示。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment