Created
October 19, 2016 13:57
-
-
Save BrandonShega/e1426e0ead32f6936bd39fc94b5448ff to your computer and use it in GitHub Desktop.
Table View Insets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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