0

I'm using AutoLayout for my app designing. For one of my HeaderView, I had set it's height using AspectRatio.

Now when main view does not contain any data then HeaderView height should be 0 and when data is added it should be again set to the original aspect ratio.

Can we change the aspect ratio as we change the constant value of any constraint. I tried below code, but it's not working properly.

@IBOutlet weak var constOptions_Height: NSLayoutConstraint!
if arrData.count == 0 {
    constOptions_Height.constant = 0.0
}
else{
    constOptions_Height.constant = 50.0
}
Ankita Shah
  • 2,178
  • 3
  • 25
  • 42
  • i think you need to change the multiplier value not the constant. constOptions_Height.multiplier = cgfloatValue – Rishabh Aug 02 '17 at 07:29
  • @Rishabh Multiplier is a get only property. – Ankita Shah Aug 02 '17 at 07:38
  • then you could try to recreate that constraint in order to modify the multiplier, pretty much like we assign the frame to alter position of views. Or simply create that constraint from code instead of storyboard. https://stackoverflow.com/questions/31334017/how-to-set-aspect-ratio-constraint-programmatically-in-ios – Rishabh Aug 02 '17 at 07:44
  • also check this: https://stackoverflow.com/questions/37294522/ios-change-the-multiplier-of-constraint-by-swift – Rishabh Aug 02 '17 at 07:52

1 Answers1

4

You can use following approach

Give two constraints. One for aspect ratio and another for height.

Make the two active alternatively like

constOptions_Aspect.isActive = false
constOptions_Height.isActive = true 

You can give height zero, in height constant.

Mohammad Sadiq
  • 5,070
  • 28
  • 29
  • Actually, you don't have to change `constOptions_Aspect` if it has lower priority than `constOptions_Height`. For exaple, if `constOptions_Aspect` has priority `999` and `constOptions_Height` has priority `1001`, then setting `constOptions_Height.isActive = true` will be enough. – Sulthan Aug 02 '17 at 08:43
  • Yes right. With different priorities also we can achieve the same. – Mohammad Sadiq Aug 02 '17 at 08:51