Last active
June 16, 2016 06:44
-
-
Save quangtqag/9d5aa0adc013d3f5170af7e9a84036e7 to your computer and use it in GitHub Desktop.
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
// iOS 9 or Above | |
// Reference: https://www.raywenderlich.com/125718/coding-auto-layout | |
// | |
avatarView.translatesAutoresizingMaskIntoConstraints = false | |
avatarView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true | |
avatarView.leadingAnchor.constraintEqualToAnchor(view.layoutMarginsGuide.leadingAnchor).active = true | |
chapterLabel.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true | |
avatarView.bottomAnchor.constraintEqualToAnchor(chapterLabel.topAnchor,constant: -10).active = true | |
chapterLabel.setContentHuggingPriority(UILayoutPriorityRequired, forAxis: .Vertical) | |
chapterLabel.setContentCompressionResistancePriority(UILayoutPriorityRequired, forAxis: .Vertical) | |
NSLayoutConstraint.activateConstraints([ | |
imageViewTop, imageViewBottom, | |
labelBottom, | |
socialMediaTrailing]) | |
override func intrinsicContentSize() -> CGSize { | |
return CGSize(width: UIViewNoIntrinsicMetric, height: 100) | |
} | |
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { | |
super.traitCollectionDidChange(previousTraitCollection) | |
if traitCollection.horizontalSizeClass == .Regular { | |
NSLayoutConstraint.deactivateConstraints(compactConstraints) | |
NSLayoutConstraint.activateConstraints(regularConstraints) | |
socialMediaView.axis = .Horizontal | |
} else { | |
NSLayoutConstraint.deactivateConstraints(regularConstraints) | |
NSLayoutConstraint.activateConstraints(compactConstraints) | |
socialMediaView.axis = .Vertical | |
} | |
} | |
override func updateConstraints() { | |
super.updateConstraints() | |
var aspectRatio: CGFloat = 1 | |
if let image = image { | |
aspectRatio = image.size.width / image.size.height | |
} | |
aspectRatioConstraint?.active = false | |
aspectRatioConstraint = imageView.widthAnchor.constraintEqualToAnchor(imageView.heightAnchor,multiplier: aspectRatio) | |
aspectRatioConstraint?.active = true | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
if bounds.height < socialMediaView.bounds.height { | |
socialMediaView.alpha = 0 | |
} else { | |
socialMediaView.alpha = 1 | |
} | |
if imageView.bounds.height < 30 { | |
imageView.alpha = 0 | |
} else { | |
imageView.alpha = 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment