2

I have a height outlet for a view. It is set to 55.In a tableview i have used it. Some where i require a static height and some where it does not require a height. So i want to remove this static height from my code.How can i achieve this?is it possible?

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(heightIsStatic.count> 0)
        {
     //here i require a static height and is working fine as height can   
     //be printed as 55
        }
        else
        {
            self.ViewHeight.active = false
            print(self.ViewHeight.constant)
        }
    }

Even i have set active to false it is still showing static height constraint.Can i remove that static height in else case??

jenny sam
  • 191
  • 3
  • 13
  • `ViewHeight.constant = 65 //StaticValue ` do like this. – dahiya_boy Oct 05 '17 at 05:26
  • i know this? i am asking about how can i remove this constraint so that it does not works for else condition. – jenny sam Oct 05 '17 at 05:27
  • For more clearance of question can you show me Image of your screen. – dahiya_boy Oct 05 '17 at 05:31
  • If you do not want height, then use `ViewHeight.constant = 0` in else condition, – dahiya_boy Oct 05 '17 at 05:32
  • Even in else case the height is still showing 55. That is all my problem and i want to remove it by any way. – jenny sam Oct 05 '17 at 05:33
  • you need to refer this https://stackoverflow.com/questions/27494542/when-can-i-activate-deactivate-layout-constraints @jennysam –  Oct 05 '17 at 05:33
  • @dahiya_boy Are you serious?What is impact of removing the constraint and setting the ViewHeight.constant = 0 ???? – jenny sam Oct 05 '17 at 05:36
  • You want to remove height constraint constant in else part so it can be dynamic size, Right ? or there is another reason for doing this ? – Varun Naharia Oct 05 '17 at 05:38
  • When you set `active` to false. The constraint is no longer active. That is for sure. Your view's height is not constrained to 55 anymore. But that doesn't prevent you from accessing the constraint's `constant` property. It still has a constant of 55, but it's no longer active. – Sweeper Oct 05 '17 at 05:39
  • @jennysam why you removing the constraints? pls explain. A view is randomly available on screen doesnt make sense. – dahiya_boy Oct 05 '17 at 05:39
  • @VarunNaharia yes you are right. It has dynamic size – jenny sam Oct 05 '17 at 05:40

2 Answers2

2

If you want the dynamic size for else part then you have to remove outlet of constraint then in your code you have to find the constraint and then change it's constant or remove if needed

for constraint in view.constraints {
    if(constraint.firstAttribute == NSLayoutAttribute.height)
    {
         constraint.constant = 55
    }
}
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
0

If I understood your question correctly, pls try this (assuming the constraints for your cells are correct)

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
 if (indexPath.row == 1) { //change this with the row number
  return 55.0
 }

 return UITableViewAutomaticDimension
}

Pls comment if you dont have a specific row and I will edit my answer. Thanks!

dhin
  • 460
  • 5
  • 12