Created
September 4, 2018 18:41
-
-
Save jacobmbarnard/207d611150e9efabaef6ebf6537e22f8 to your computer and use it in GitHub Desktop.
Practical Programmatic Auto Layout for UITableViewCell in Swift 4.1
This file contains 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 | |
public class MyAutoLayoutTableViewCell: UITableViewCell { | |
var myRedLabel: UILabel | |
var myGreenLabel: UILabel | |
var myBlueLabel: UILabel | |
override public init(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
myRedLabel = UILabel(frame: CGRect.zero) | |
myGreenLabel = UILabel(frame: CGRect.zero) | |
myBlueLabel = UILabel(frame: CGRect.zero) | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
setupContentView() | |
setupSubviewsForAutoLayout() | |
} | |
required public init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
fileprivate func setupContentView() { | |
let width = UIScreen.main.bounds.width | |
contentView.frame = CGRect(x: 0.0, y: 0.0, width: width, height: 60.0) | |
} | |
fileprivate func setupSubviewsForAutoLayout() { | |
let views: [String: Any] = [ | |
"myRedLabel": myRedLabel, | |
"myGreenLabel": myGreenLabel, | |
"myBlueLabel": myBlueLabel | |
] | |
setupMyRedLabel(for: views) | |
setupMyGreenLabel(for: views) | |
setupMyBlueLabel(for: views) | |
} | |
fileprivate func setupMyRedLabel(for views: [String: Any]) { | |
var constraints: [NSLayoutConstraint] = [] | |
contentView.addSubview(myRedLabel) | |
myRedLabel.layoutMargins = UIEdgeInsets.zero | |
myRedLabel.text = "Red Label" | |
myRedLabel.font = UIFont.boldSystemFont(ofSize: 15.0) | |
myRedLabel.textColor = .red | |
myRedLabel.translatesAutoresizingMaskIntoConstraints = false | |
let myRedLabelHorizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[myRedLabel]", options: [], metrics: nil, views: views) | |
let myRedLabelVerticalConstraint = NSLayoutConstraint.constraints( withVisualFormat: "V:|-5-[myRedLabel(==18)]", options:[], metrics: nil, views: views) | |
constraints += myRedLabelHorizontalConstraint + myRedLabelVerticalConstraint | |
NSLayoutConstraint.activate(constraints) | |
} | |
fileprivate func setupMyGreenLabel(for views: [String: Any]) { | |
var constraints: [NSLayoutConstraint] = [] | |
contentView.addSubview(myGreenLabel) | |
myGreenLabel.layoutMargins = UIEdgeInsets.zero | |
myGreenLabel.text = "Green Label" | |
myGreenLabel.font = UIFont.boldSystemFont(ofSize: 15.0) | |
myGreenLabel.textColor = .green | |
myGreenLabel.translatesAutoresizingMaskIntoConstraints = false | |
let myGreenLabelHorizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:[myRedLabel]-8-[myGreenLabel]", options: [], metrics: nil, views: views) | |
let myGreenLabelVerticalConstraint = NSLayoutConstraint.constraints( withVisualFormat: "V:|-5-[myGreenLabel(==18)]", options:[], metrics: nil, views: views) | |
constraints += myGreenLabelHorizontalConstraint + myGreenLabelVerticalConstraint | |
NSLayoutConstraint.activate(constraints) | |
} | |
fileprivate func setupMyBlueLabel(for views: [String: Any]) { | |
var constraints: [NSLayoutConstraint] = [] | |
contentView.addSubview(myBlueLabel) | |
myBlueLabel.layoutMargins = UIEdgeInsets.zero | |
myBlueLabel.text = "Blue Label" | |
myBlueLabel.font = UIFont.boldSystemFont(ofSize: 15.0) | |
myBlueLabel.textColor = .blue | |
myBlueLabel.translatesAutoresizingMaskIntoConstraints = false | |
let myGreenLabelHorizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[myBlueLabel]", options: [], metrics: nil, views: views) | |
let myGreenLabelVerticalConstraint = NSLayoutConstraint.constraints( withVisualFormat: "V:[myRedLabel]-5-[myBlueLabel]", options:[], metrics: nil, views: views) | |
constraints += myGreenLabelHorizontalConstraint + myGreenLabelVerticalConstraint | |
NSLayoutConstraint.activate(constraints) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you merely want to see this cell in action on launch in a new iPhone project, a quick way to do that is:
storyboard
file.Info.plist
.AppDelegate
to look like this:and make a simple
UITableViewController
subclass like so:Build and run!