add container for titleLable
This commit is contained in:
parent
8a00fe0d90
commit
0a537b62d9
|
|
@ -16,6 +16,7 @@ class SegmentioCell: UICollectionViewCell {
|
|||
var verticalSeparatorView: UIView?
|
||||
var segmentTitleLabel: UILabel?
|
||||
var segmentImageView: UIImageView?
|
||||
var containerView: UIView?
|
||||
|
||||
var topConstraint: NSLayoutConstraint?
|
||||
var bottomConstraint: NSLayoutConstraint?
|
||||
|
|
@ -61,18 +62,25 @@ class SegmentioCell: UICollectionViewCell {
|
|||
contentView.addSubview(segmentImageView)
|
||||
}
|
||||
|
||||
containerView = UIView(frame: CGRectZero)
|
||||
if let containerView = containerView {
|
||||
contentView.addSubview(containerView)
|
||||
}
|
||||
|
||||
segmentTitleLabel = UILabel(frame: CGRectZero)
|
||||
if let segmentTitleLabel = segmentTitleLabel {
|
||||
contentView.addSubview(segmentTitleLabel)
|
||||
if let segmentTitleLabel = segmentTitleLabel, containerView = containerView {
|
||||
containerView.addSubview(segmentTitleLabel)
|
||||
}
|
||||
|
||||
segmentImageView?.translatesAutoresizingMaskIntoConstraints = false
|
||||
segmentTitleLabel?.translatesAutoresizingMaskIntoConstraints = false
|
||||
containerView?.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
segmentImageView?.layer.masksToBounds = true
|
||||
segmentTitleLabel?.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
|
||||
|
||||
setupConstraintsForSubviews()
|
||||
setupContainerConstraints()
|
||||
addVerticalSeparator()
|
||||
}
|
||||
|
||||
|
|
@ -131,6 +139,39 @@ class SegmentioCell: UICollectionViewCell {
|
|||
|
||||
// MARK: - Private functions
|
||||
|
||||
func setupContainerConstraints() {
|
||||
guard let segmentTitleLabel = segmentTitleLabel else {
|
||||
return
|
||||
}
|
||||
guard let containerView = containerView else {
|
||||
return
|
||||
}
|
||||
|
||||
let segmentTitleLabelHorizontalCenterConstraint =
|
||||
NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
attribute: .CenterX,
|
||||
relatedBy: .Equal,
|
||||
toItem: containerView,
|
||||
attribute: .CenterX,
|
||||
multiplier: 1,
|
||||
constant: 0.0
|
||||
)
|
||||
|
||||
let segmentTitleLabelVerticalCenterConstraint =
|
||||
NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
attribute: .CenterY,
|
||||
relatedBy: .Equal,
|
||||
toItem: containerView,
|
||||
attribute: .CenterY,
|
||||
multiplier: 1,
|
||||
constant: 0.0
|
||||
)
|
||||
|
||||
addConstraints([segmentTitleLabelHorizontalCenterConstraint, segmentTitleLabelVerticalCenterConstraint])
|
||||
}
|
||||
|
||||
private func setupContent(content content: SegmentioItem) {
|
||||
if style.isWithImage() {
|
||||
segmentImageView?.contentMode = options.imageContentMode
|
||||
|
|
|
|||
|
|
@ -17,11 +17,14 @@ final class SegmentioCellWithImageAfterLabel: SegmentioCell {
|
|||
guard let segmentTitleLabel = segmentTitleLabel else {
|
||||
return
|
||||
}
|
||||
guard let containerView = containerView else {
|
||||
return
|
||||
}
|
||||
|
||||
let metrics = ["labelHeight": segmentTitleLabelHeight]
|
||||
let views = [
|
||||
"segmentImageView": segmentImageView,
|
||||
"segmentTitleLabel": segmentTitleLabel
|
||||
"containerView": containerView
|
||||
]
|
||||
|
||||
// main constraints
|
||||
|
|
@ -34,16 +37,15 @@ final class SegmentioCellWithImageAfterLabel: SegmentioCell {
|
|||
NSLayoutConstraint.activateConstraints(segmentImageViewVerticalConstraint)
|
||||
|
||||
let contentViewHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||
"|-(>=10)-[segmentTitleLabel]-[segmentImageView(labelHeight)]-|",
|
||||
"|-[containerView]-[segmentImageView(labelHeight)]-|",
|
||||
options: [.AlignAllCenterY],
|
||||
metrics: metrics,
|
||||
views: views)
|
||||
NSLayoutConstraint.activateConstraints(contentViewHorizontalConstraints)
|
||||
|
||||
// custom constraints
|
||||
|
||||
topConstraint = NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
item: containerView,
|
||||
attribute: .Top,
|
||||
relatedBy: .Equal,
|
||||
toItem: contentView,
|
||||
|
|
@ -57,7 +59,7 @@ final class SegmentioCellWithImageAfterLabel: SegmentioCell {
|
|||
item: contentView,
|
||||
attribute: .Bottom,
|
||||
relatedBy: .Equal,
|
||||
toItem: segmentTitleLabel,
|
||||
toItem: containerView,
|
||||
attribute: .Bottom,
|
||||
multiplier: 1,
|
||||
constant: padding
|
||||
|
|
|
|||
|
|
@ -17,11 +17,14 @@ class SegmentioCellWithImageBeforeLabel: SegmentioCell {
|
|||
guard let segmentTitleLabel = segmentTitleLabel else {
|
||||
return
|
||||
}
|
||||
guard let containerView = containerView else {
|
||||
return
|
||||
}
|
||||
|
||||
let metrics = ["labelHeight": segmentTitleLabelHeight]
|
||||
let views = [
|
||||
"segmentImageView": segmentImageView,
|
||||
"segmentTitleLabel": segmentTitleLabel
|
||||
"containerView": containerView
|
||||
]
|
||||
|
||||
// main constraints
|
||||
|
|
@ -34,7 +37,7 @@ class SegmentioCellWithImageBeforeLabel: SegmentioCell {
|
|||
NSLayoutConstraint.activateConstraints(segmentImageViewVerticalConstraint)
|
||||
|
||||
let contentViewHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||
"|-[segmentImageView(labelHeight)]-[segmentTitleLabel]-(>=10)-|",
|
||||
"|-[segmentImageView(labelHeight)]-[containerView]-|",
|
||||
options: [.AlignAllCenterY],
|
||||
metrics: metrics,
|
||||
views: views)
|
||||
|
|
@ -43,7 +46,7 @@ class SegmentioCellWithImageBeforeLabel: SegmentioCell {
|
|||
// custom constraints
|
||||
|
||||
topConstraint = NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
item: containerView,
|
||||
attribute: .Top,
|
||||
relatedBy: .Equal,
|
||||
toItem: contentView,
|
||||
|
|
@ -57,7 +60,7 @@ class SegmentioCellWithImageBeforeLabel: SegmentioCell {
|
|||
item: contentView,
|
||||
attribute: .Bottom,
|
||||
relatedBy: .Equal,
|
||||
toItem: segmentTitleLabel,
|
||||
toItem: containerView,
|
||||
attribute: .Bottom,
|
||||
multiplier: 1,
|
||||
constant: padding
|
||||
|
|
|
|||
|
|
@ -17,11 +17,14 @@ class SegmentioCellWithImageOverLabel: SegmentioCell {
|
|||
guard let segmentTitleLabel = segmentTitleLabel else {
|
||||
return
|
||||
}
|
||||
guard let containerView = containerView else {
|
||||
return
|
||||
}
|
||||
|
||||
let metrics = ["labelHeight": segmentTitleLabelHeight]
|
||||
let views = [
|
||||
"segmentImageView": segmentImageView,
|
||||
"segmentTitleLabel": segmentTitleLabel
|
||||
"containerView": containerView
|
||||
]
|
||||
|
||||
// main constraints
|
||||
|
|
@ -34,30 +37,17 @@ class SegmentioCellWithImageOverLabel: SegmentioCell {
|
|||
NSLayoutConstraint.activateConstraints(segmentImageViewHorizontConstraint)
|
||||
|
||||
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||
"|-(>=10)-[segmentTitleLabel]-(>=10)-|",
|
||||
"|-[containerView]-|",
|
||||
options: [.AlignAllCenterX],
|
||||
metrics: nil,
|
||||
views: [
|
||||
"segmentTitleLabel": segmentTitleLabel
|
||||
]
|
||||
views: views
|
||||
)
|
||||
|
||||
let segmentTitleLabelHorizontalCenterConstraint =
|
||||
NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
attribute: .CenterX,
|
||||
relatedBy: .Equal,
|
||||
toItem: segmentTitleLabel.superview,
|
||||
attribute: .CenterX,
|
||||
multiplier: 1,
|
||||
constant: 0.0
|
||||
)
|
||||
|
||||
addConstraint(segmentTitleLabelHorizontalCenterConstraint)
|
||||
|
||||
NSLayoutConstraint.activateConstraints(segmentTitleLabelHorizontConstraint)
|
||||
|
||||
let contentViewVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||
"V:[segmentImageView]-[segmentTitleLabel(labelHeight)]",
|
||||
"V:[segmentImageView]-[containerView(labelHeight)]",
|
||||
options: [],
|
||||
metrics: metrics,
|
||||
views: views)
|
||||
|
|
@ -80,7 +70,7 @@ class SegmentioCellWithImageOverLabel: SegmentioCell {
|
|||
item: contentView,
|
||||
attribute: .Bottom,
|
||||
relatedBy: .Equal,
|
||||
toItem: segmentTitleLabel,
|
||||
toItem: containerView,
|
||||
attribute: .Bottom,
|
||||
multiplier: 1,
|
||||
constant: padding
|
||||
|
|
|
|||
|
|
@ -17,11 +17,14 @@ class SegmentioCellWithImageUnderLabel: SegmentioCell {
|
|||
guard let segmentTitleLabel = segmentTitleLabel else {
|
||||
return
|
||||
}
|
||||
guard let containerView = containerView else {
|
||||
return
|
||||
}
|
||||
|
||||
let metrics = ["labelHeight": segmentTitleLabelHeight]
|
||||
let views = [
|
||||
"segmentImageView": segmentImageView,
|
||||
"segmentTitleLabel": segmentTitleLabel
|
||||
"containerView": containerView
|
||||
]
|
||||
|
||||
// main constraints
|
||||
|
|
@ -34,29 +37,15 @@ class SegmentioCellWithImageUnderLabel: SegmentioCell {
|
|||
NSLayoutConstraint.activateConstraints(segmentImageViewHorizontConstraint)
|
||||
|
||||
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||
"|-(>=10)-[segmentTitleLabel]-(>=10)-|",
|
||||
"|-[containerView]-|",
|
||||
options: [.AlignAllCenterX],
|
||||
metrics: nil,
|
||||
views: [
|
||||
"segmentTitleLabel": segmentTitleLabel
|
||||
]
|
||||
views: views
|
||||
)
|
||||
let segmentTitleLabelHorizontalCenterConstraint =
|
||||
NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
attribute: .CenterX,
|
||||
relatedBy: .Equal,
|
||||
toItem: segmentTitleLabel.superview,
|
||||
attribute: .CenterX,
|
||||
multiplier: 1,
|
||||
constant: 0.0
|
||||
)
|
||||
|
||||
addConstraint(segmentTitleLabelHorizontalCenterConstraint)
|
||||
NSLayoutConstraint.activateConstraints(segmentTitleLabelHorizontConstraint)
|
||||
|
||||
let contentViewVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||
"V:[segmentTitleLabel(labelHeight)]-[segmentImageView]",
|
||||
"V:[containerView(labelHeight)]-[segmentImageView]",
|
||||
options: [],
|
||||
metrics: metrics,
|
||||
views: views)
|
||||
|
|
@ -65,7 +54,7 @@ class SegmentioCellWithImageUnderLabel: SegmentioCell {
|
|||
// custom constraints
|
||||
|
||||
topConstraint = NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
item: containerView,
|
||||
attribute: .Top,
|
||||
relatedBy: .Equal,
|
||||
toItem: contentView,
|
||||
|
|
|
|||
|
|
@ -15,36 +15,26 @@ final class SegmentioCellWithLabel: SegmentioCell {
|
|||
return
|
||||
}
|
||||
|
||||
let views = ["segmentTitleLabel": segmentTitleLabel]
|
||||
guard let containerView = containerView else {
|
||||
return
|
||||
}
|
||||
|
||||
let views = ["containerView": containerView]
|
||||
|
||||
// main constraints
|
||||
|
||||
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||
"|-(>=10)-[segmentTitleLabel]-(>=10)-|",
|
||||
"|-[containerView]-|",
|
||||
options: [.AlignAllCenterX],
|
||||
metrics: nil,
|
||||
views: [
|
||||
"segmentTitleLabel": segmentTitleLabel
|
||||
]
|
||||
views: views
|
||||
)
|
||||
let segmentTitleLabelHorizontalCenterConstraint =
|
||||
NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
attribute: .CenterX,
|
||||
relatedBy: .Equal,
|
||||
toItem: segmentTitleLabel.superview,
|
||||
attribute: .CenterX,
|
||||
multiplier: 1,
|
||||
constant: 0.0
|
||||
)
|
||||
|
||||
addConstraint(segmentTitleLabelHorizontalCenterConstraint)
|
||||
NSLayoutConstraint.activateConstraints(segmentTitleLabelHorizontConstraint)
|
||||
|
||||
// custom constraints
|
||||
|
||||
topConstraint = NSLayoutConstraint(
|
||||
item: segmentTitleLabel,
|
||||
item: containerView,
|
||||
attribute: .Top,
|
||||
relatedBy: .Equal,
|
||||
toItem: contentView,
|
||||
|
|
@ -58,7 +48,7 @@ final class SegmentioCellWithLabel: SegmentioCell {
|
|||
item: contentView,
|
||||
attribute: .Bottom,
|
||||
relatedBy: .Equal,
|
||||
toItem: segmentTitleLabel,
|
||||
toItem: containerView,
|
||||
attribute: .Bottom,
|
||||
multiplier: 1,
|
||||
constant: padding
|
||||
|
|
|
|||
Loading…
Reference in New Issue