Skip to content

Instantly share code, notes, and snippets.

@florieger
Last active March 12, 2019 09:09
Show Gist options
  • Save florieger/d54f9c5fda53a22009e2162efc2cc00e to your computer and use it in GitHub Desktop.
Save florieger/d54f9c5fda53a22009e2162efc2cc00e to your computer and use it in GitHub Desktop.
Swift ViewController without Storyboard / XIB.
class ACViewController: UIViewController {
let frame: CGRect
let childViewController: UIViewController
var imageView: UIImageView?
// MARK: Init
init(frame: CGRect) {
self.frame = frame
childViewController = UIViewController()
super.init(nibName: nil, bundle: nil)
title = "My Title"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.frame = frame
view.backgroundColor = UIColor.redColor()
let imageView = UIImageView()
imageView.frame = CGRectMake(20, 20, 100, 100)
imageView.image = UIImage(named: "MemoryConsumingImage")
view.addSubview(imageView)
self.imageView = imageView
addChildViewController(childViewController)
view.addSubview(childViewController.view)
childViewController.didMoveToParentViewController(self)
}
// MARK: Memory Warnings
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
if isViewLoaded() && view.window == nil {
self.view = nil
}
if (!self.isViewLoaded()) {
// Release subviews
self.imageView = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment