From 62612dfe3bb2c7871e6ada544011c2f7ba6363be Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 13 Dec 2017 22:22:47 +0800 Subject: [PATCH 1/3] Fix messageLabel layout issue --- Sources/Views/Cells/TextMessageCell.swift | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Sources/Views/Cells/TextMessageCell.swift b/Sources/Views/Cells/TextMessageCell.swift index df312216..f635ab98 100644 --- a/Sources/Views/Cells/TextMessageCell.swift +++ b/Sources/Views/Cells/TextMessageCell.swift @@ -44,7 +44,7 @@ open class TextMessageCell: MessageCollectionViewCell { super.apply(layoutAttributes) if let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes { messageLabel.configure { - messageLabel.frame = attributes.messageContainerFrame + messageLabel.frame = messageContainerView.bounds messageLabel.textInsets = attributes.messageLabelInsets messageLabel.font = attributes.messageLabelFont } @@ -60,11 +60,6 @@ open class TextMessageCell: MessageCollectionViewCell { open override func setupSubviews() { super.setupSubviews() messageContainerView.addSubview(messageLabel) - setupConstraints() - } - - open func setupConstraints() { - messageLabel.fillSuperview() } open func configure(_ message: MessageType, _ textColor: UIColor, _ detectors: [DetectorType]) { From cc5642344027ba89ad5da08d1d6c89e181f47de0 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 14 Dec 2017 15:11:23 +0800 Subject: [PATCH 2/3] Fix MediaMessageCell's PlayButtonView layout issue --- Sources/Views/Cells/MediaMessageCell.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Views/Cells/MediaMessageCell.swift b/Sources/Views/Cells/MediaMessageCell.swift index d84761ba..340f5b4e 100644 --- a/Sources/Views/Cells/MediaMessageCell.swift +++ b/Sources/Views/Cells/MediaMessageCell.swift @@ -33,6 +33,8 @@ open class MediaMessageCell: MessageCollectionViewCell { open lazy var playButtonView: PlayButtonView = { let playButtonView = PlayButtonView() playButtonView.frame.size = CGSize(width: 35, height: 35) + playButtonView.center = messageContainerView.center + playButtonView.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin] return playButtonView }() @@ -42,7 +44,6 @@ open class MediaMessageCell: MessageCollectionViewCell { open func setupConstraints() { imageView.fillSuperview() - playButtonView.centerInSuperview() } open override func setupSubviews() { From a9f027606edd6bf03ee5aea8ce017bf53efcf083 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 14 Dec 2017 20:47:51 +0800 Subject: [PATCH 3/3] Change messageLabel and playButtonView layout from frame to auto layout --- Sources/Extensions/UIView+Extensions.swift | 11 +++++ Sources/Views/Cells/MediaMessageCell.swift | 5 +-- Sources/Views/Cells/TextMessageCell.swift | 6 ++- Sources/Views/PlayButtonView.swift | 50 ++++++++-------------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/Sources/Extensions/UIView+Extensions.swift b/Sources/Extensions/UIView+Extensions.swift index 7a83707f..21d4c66d 100644 --- a/Sources/Extensions/UIView+Extensions.swift +++ b/Sources/Extensions/UIView+Extensions.swift @@ -52,6 +52,17 @@ extension UIView { ] NSLayoutConstraint.activate(constraints) } + + func constraint(equalTo size: CGSize) { + guard superview != nil else { return } + translatesAutoresizingMaskIntoConstraints = false + let constraints: [NSLayoutConstraint] = [ + widthAnchor.constraint(equalToConstant: size.width), + heightAnchor.constraint(equalToConstant: size.height) + ] + NSLayoutConstraint.activate(constraints) + + } @discardableResult func addConstraints(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { diff --git a/Sources/Views/Cells/MediaMessageCell.swift b/Sources/Views/Cells/MediaMessageCell.swift index 340f5b4e..85df89d0 100644 --- a/Sources/Views/Cells/MediaMessageCell.swift +++ b/Sources/Views/Cells/MediaMessageCell.swift @@ -32,9 +32,6 @@ open class MediaMessageCell: MessageCollectionViewCell { open lazy var playButtonView: PlayButtonView = { let playButtonView = PlayButtonView() - playButtonView.frame.size = CGSize(width: 35, height: 35) - playButtonView.center = messageContainerView.center - playButtonView.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin] return playButtonView }() @@ -44,6 +41,8 @@ open class MediaMessageCell: MessageCollectionViewCell { open func setupConstraints() { imageView.fillSuperview() + playButtonView.centerInSuperview() + playButtonView.constraint(equalTo: CGSize(width: 35, height: 35)) } open override func setupSubviews() { diff --git a/Sources/Views/Cells/TextMessageCell.swift b/Sources/Views/Cells/TextMessageCell.swift index f635ab98..a1a24b15 100644 --- a/Sources/Views/Cells/TextMessageCell.swift +++ b/Sources/Views/Cells/TextMessageCell.swift @@ -44,7 +44,6 @@ open class TextMessageCell: MessageCollectionViewCell { super.apply(layoutAttributes) if let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes { messageLabel.configure { - messageLabel.frame = messageContainerView.bounds messageLabel.textInsets = attributes.messageLabelInsets messageLabel.font = attributes.messageLabelFont } @@ -60,6 +59,11 @@ open class TextMessageCell: MessageCollectionViewCell { open override func setupSubviews() { super.setupSubviews() messageContainerView.addSubview(messageLabel) + setupConstraints() + } + + open func setupConstraints() { + messageLabel.fillSuperview() } open func configure(_ message: MessageType, _ textColor: UIColor, _ detectors: [DetectorType]) { diff --git a/Sources/Views/PlayButtonView.swift b/Sources/Views/PlayButtonView.swift index 1296c83c..ec650b9b 100644 --- a/Sources/Views/PlayButtonView.swift +++ b/Sources/Views/PlayButtonView.swift @@ -30,29 +30,8 @@ open class PlayButtonView: UIView { open let triangleView = UIView() - private var triangleHeightConstraint: NSLayoutConstraint? - private var triangleWidthConstraint: NSLayoutConstraint? private var triangleCenterXConstraint: NSLayoutConstraint? - - private var triangleViewSize: CGSize { - return CGSize(width: frame.width/2, height: frame.height/2) - } - - open override var frame: CGRect { - didSet { - updateTriangleConstraints() - applyCornerRadius() - applyTriangleMask() - } - } - - open override var bounds: CGRect { - didSet { - updateTriangleConstraints() - applyCornerRadius() - applyTriangleMask() - } - } + private var cacheFrame: CGRect = .zero // MARK: - Initializers @@ -61,8 +40,6 @@ open class PlayButtonView: UIView { setupSubviews() setupConstraints() - applyCornerRadius() - applyTriangleMask() triangleView.clipsToBounds = true triangleView.backgroundColor = .black @@ -74,6 +51,17 @@ open class PlayButtonView: UIView { } // MARK: - Methods + + open override func layoutSubviews() { + super.layoutSubviews() + + guard !cacheFrame.equalTo(frame) else { return } + cacheFrame = frame + + updateTriangleConstraints() + applyCornerRadius() + applyTriangleMask() + } private func setupSubviews() { addSubview(triangleView) @@ -82,13 +70,11 @@ open class PlayButtonView: UIView { private func setupConstraints() { triangleView.translatesAutoresizingMaskIntoConstraints = false - let centerX = triangleView.centerXAnchor.constraint(equalTo: centerXAnchor, constant: triangleViewSize.width/8) + let centerX = triangleView.centerXAnchor.constraint(equalTo: centerXAnchor) let centerY = triangleView.centerYAnchor.constraint(equalTo: centerYAnchor) - let width = triangleView.widthAnchor.constraint(equalToConstant: triangleViewSize.width) - let height = triangleView.heightAnchor.constraint(equalToConstant: triangleViewSize.height) + let width = triangleView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.5) + let height = triangleView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.5) - triangleWidthConstraint = width - triangleHeightConstraint = height triangleCenterXConstraint = centerX NSLayoutConstraint.activate([centerX, centerY, width, height]) @@ -113,13 +99,11 @@ open class PlayButtonView: UIView { } private func updateTriangleConstraints() { - triangleWidthConstraint?.constant = triangleViewSize.width - triangleHeightConstraint?.constant = triangleViewSize.height - triangleCenterXConstraint?.constant = triangleViewSize.width/8 + triangleCenterXConstraint?.constant = frame.width/8 } private func applyTriangleMask() { - let rect = CGRect(origin: .zero, size: triangleViewSize) + let rect = CGRect(origin: .zero, size: triangleView.bounds.size) triangleView.layer.mask = triangleMask(for: rect) }