robb / Cartography

A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:
Other
7.35k stars 524 forks source link

dynamic view height #223

Open liuruxian opened 8 years ago

liuruxian commented 8 years ago

I use Cartography to layout my cell, But how can I get change the view of height with contrain

constrain(self.bgView!, self.introduce!) { (view1, view2) in
        view1.height == 20
        view1.left == view1.superview!.left+10
        view1.right == view1.superview!.right-10
        view1.top == view1.superview!.top+10

        view2.left == view1.left
        view2.right == view1.right
        view2.top == view1.bottom + 10
        view2.bottom == view2.superview!.bottom - 10
    }

and then , the bgview of height will be changed code: constrain(self.bgView!) { view in let viewHeight:CGFloat = 80 view.height == viewHeight }

it recevie the layout of error

morgz commented 8 years ago

what type is viewHeight? Double etc. won't work it needs to be a CGFloat. You could try

view.height == CGFloat(viewHeight)

galenom commented 7 years ago

I was having similar issue, you have to store the original constraints inside of a ConstraintGroup. Then re-state all of the constraints + the one you want updated. Like this: ` let group = constrain(self.bgView!, self.introduce!) { (view1, view2) in view1.height == 20 view1.left == view1.superview!.left+10 view1.right == view1.superview!.right-10 view1.top == view1.superview!.top+10

    view2.left == view1.left
    view2.right == view1.right
    view2.top == view1.bottom + 10
    view2.bottom == view2.superview!.bottom - 10
}`

Then: ` constrain(self.bgView!, self.introduce!, replace: group) { (view1, view2) in view1.height == 80 view1.left == view1.superview!.left+10 view1.right == view1.superview!.right-10 view1.top == view1.superview!.top+10

    view2.left == view1.left
    view2.right == view1.right
    view2.top == view1.bottom + 10
    view2.bottom == view2.superview!.bottom - 10
}`
strangeliu commented 7 years ago

before change the height constrain you need to replace the old one