CosmicMind/Material

Divider AutoLayout

o-nnerb opened this issue · 8 comments

It is possible to make divider moves with autoLayout? I have overridden Divider class and did that using SnapKit.

self.snp.removeConstraints()
self.snp.makeConstraints { make in
make.bottom.leading.trailing.equalTo(0)
make.height.equalTo(thickness)
}

Hey! Not sure I understood what you are trying to achieve. Can you elaborate on it more?

I was using the default Divider from Material and sometimes, when the views changes a lot the divider wasn't following the auto layout instructions. The problem occurred when I was using the Skeleton, but since I overridden the Divider class with putting SnapKit, now I didn't have any problem

//
//  Divider.swift
//

import Foundation
import UIKit

class Divider: UIView {

    static func from(view: UIView) -> Divider? {
        guard let divider = view.subviews.first(where: { $0 is Divider }) as? Divider else {
            return nil
        }

        return divider
    }

    static func orCreate(view: UIView) -> Divider {
        if let divider = Divider.from(view: view) {
            return divider
        }

        let divider = Divider()
        view.addSubview(divider)

        divider.update(thickness: 1)

        return divider
    }

    private(set) var thickness: CGFloat = 1
    func update(thickness: CGFloat) {
        self.thickness = thickness

        self.snp.removeConstraints()
        self.snp.makeConstraints { make in
            make.bottom.leading.trailing.equalTo(0)
            make.height.equalTo(thickness)
        }
    }
}

extension UIView {

    @IBInspectable
    var dividerColor: UIColor? {
        get {
            return Divider.from(view: self)?.backgroundColor
        }

        set {
            Divider.orCreate(view: self).backgroundColor = newValue
        }
    }

    @IBInspectable
    var isDividerHidden: Bool {
        get {
            return Divider.from(view: self) == nil
        }

        set {
            if newValue {
                Divider.from(view: self)?.isHidden = true
                return
            }

            Divider.orCreate(view: self).isHidden = false
        }
    }

    @IBInspectable
    var dividerThickness: CGFloat {
        get {
            return Divider.from(view: self)?.thickness ?? 0.0
        }

        set {
            guard newValue > 0 else {
                Divider.from(view: self)?.isHidden = true
                return
            }

            Divider.orCreate(view: self).update(thickness: newValue)
        }

    }
}

Are you sure that you were calling self.layoutDivider() in layoutSubviews() of your UIView subview?

Would love to hear the feedback after you try it out @brennobemoura!

Follow pull request #1262

@OrkhanAlikhanov if I call layoutDivider() it works but I have to create a IBOutlet just for that and override viewDidLayoutSubviews()

Captura de Tela 2019-09-08 às 10 41 03

@brennobemoura We are going to revisit the divider in our next update. Closing this ticket for now :) thank you for bringing it to our attention.