Skip to content

Instantly share code, notes, and snippets.

@BrandonShega
Created October 19, 2016 13:57
Show Gist options
  • Save BrandonShega/e1426e0ead32f6936bd39fc94b5448ff to your computer and use it in GitHub Desktop.
Save BrandonShega/e1426e0ead32f6936bd39fc94b5448ff to your computer and use it in GitHub Desktop.
Table View Insets
import UIKit
import PlaygroundSupport
class MyViewController: UIViewController {
let tableView: UITableView
init() {
tableView = UITableView()
super.init(nibName: nil, bundle: nil)
setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
}
override func viewDidLoad() {
super.viewDidLoad()
automaticallyAdjustsScrollViewInsets = false
}
private func setup() {
tableView.delegate = self
tableView.dataSource = self
view.backgroundColor = .white
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
extension MyViewController: UITableViewDelegate {
}
extension MyViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
}
let vc = MyViewController()
PlaygroundPage.current.liveView = vc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment