intuit / CardParts

A reactive, card-based UI framework built on UIKit for iOS developers.
Other
2.52k stars 225 forks source link

Using custom card (with embedded view + autolayout) freezes unless CardCellMargins left/right not set to 0 #85

Open st34m3dr1c3 opened 6 years ago

st34m3dr1c3 commented 6 years ago

First off, I want to say that this library is fantastic!

On to the problem: I am using a stack of custom cards, one of which simply has a chart view embedded inside (taken from the iOS Charts library). However, using this card will cause the app to freeze unless I set the left and right margins to nonzero in the CardsViewController, e.g. with the following application:


let theme = CardPartsMintTheme()
        theme.cardCellMargins = UIEdgeInsets(top: 100, left: 15, bottom: 100, right: 15)
        theme.cardsViewContentInsetTop = 15
        theme.cardsLineSpacing = 15
        theme.apply()

However, doing so means that the cards cannot hug the edge of the screen. As mentioned previously, setting the insets to default (0 for left and right) causes the app to freeze.

See snippet for my custom card below, which has a chart embedded inside:

public class PAChartCardPartView : UIView, CardPartView {
var chart:CombinedChartView?

public init() {
        super.init(frame: CGRect.zero)
        self.chart = CombinedChartView(frame: CGRect.zero)
        self.chart?.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(self.chart!)
        self.chart?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
        self.chart?.bottomAnchor.constraint(equalTo: (self.view.bottomAnchor), constant: 0).isActive = true
        self.chart?.leadingAnchor.constraint(equalTo: (self.view.leadingAnchor), constant: 0).isActive = true
        self.chart?.trailingAnchor.constraint(equalTo: (self.view.trailingAnchor), constant: 0).isActive = true
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

Is it possible to use this card without having to use nonzero left and right insets? Any help or insight would be greatly appreciated. Thanks!

st34m3dr1c3 commented 6 years ago

I'd also like to add that making the chart the CardPart (instead of creating a general UIView CardPart and embedding the chart inside) also creates the same problem. Works fine on the default Mint theme in which the left and right insets are nonzero, but will freeze on the Turbo theme where the left and right insets are zero.

class ChartCardPartView : CombinedChartView, CardPartView {

    public var margins: UIEdgeInsets = CardParts.theme.cardPartMargins

    public init() {
        super.init(frame: CGRect.zero)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

See the resulting chart implementation in the CardsViewController:

class MainViewController: CardsViewController {

...

let chart = ChartCardPartView()

...

func setupChartView() {
       chart.addConstraint(NSLayoutConstraint(item: chart, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 300))
       chart.addConstraint(NSLayoutConstraint(item: chart, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 300))
}

override func viewDidLoad() {
...
       super.viewDidLoad()
...
       setupChartView()
}

...

}