Created
November 3, 2017 23:43
-
-
Save NickHung1982/1dcaea1f06bb4abcf5005dfdde10bebf to your computer and use it in GitHub Desktop.
customerCell
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
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
//add UIImageView | |
customImageView = UIImageView() | |
customImageView.translatesAutoresizingMaskIntoConstraints = false | |
customImageView.backgroundColor = UIColor.red | |
self.addSubview(customImageView) | |
//add UILabel | |
customLabel = UILabel() | |
customLabel.numberOfLines = 1 | |
customLabel.translatesAutoresizingMaskIntoConstraints = false | |
self.addSubview(customLabel) | |
//**** constraint to UIImageView **** | |
//top to superview's margin is 10 | |
let constraintTop = NSLayoutConstraint(item: customImageView, attribute: .topMargin, relatedBy: .equal, toItem: self, attribute: .topMargin, multiplier: 1.0, constant: 10) | |
//left to superview's margin is 10 | |
let constraintLeft = NSLayoutConstraint(item: customImageView, attribute: .leadingMargin, relatedBy: .equal, toItem: self, attribute: .leadingMargin, multiplier: 1.0, constant: 10) | |
//aspect ratio with 1:1 | |
let constraintAspectRadio = customImageView.heightAnchor.constraint(equalTo: customImageView.widthAnchor, multiplier: 1.0) | |
//setup centerY with cell | |
let constraintY = NSLayoutConstraint(item: customImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0) | |
self.addConstraints([constraintTop, constraintLeft, constraintAspectRadio, constraintY]) | |
//**** constraint to UILabel **** | |
//setup space with imageview and label | |
let horizontalCons = NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[myView]-10-[myLabel]-10-|", options: .alignAllCenterY, metrics: nil, views: ["myLabel":customLabel, "myView":customImageView]) | |
self.addConstraints(horizontalCons) | |
//setup centerY with cell | |
let labelConstraintCenterY = NSLayoutConstraint(item: customLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0) | |
//setup uilabel's height is equal 21 | |
let labelConstraintHeight = NSLayoutConstraint(item: customLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 21) | |
self.addConstraints([labelConstraintHeight,labelConstraintCenterY]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment