0

I want to make a button to active and deactivate a constraint but my problem is when i enable->disable->enable it, I get a fatal error.

Here is my code:

@IBOutlet weak var heightEmail: NSLayoutConstraint!

if alamatDomisiliSegmentedControl.selectedSegmentIndex == 1 {
    heightEmail.isActive=true
    heightEmail.constant=8
    print("tidak")
} else if alamatDomisiliSegmentedControl.selectedSegmentIndex == 0 {
    heightEmail.isActive=false
    print("ya")
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Indra Sen
  • 111
  • 1
  • 9

2 Answers2

1

I am not sure why you are using isActive. The logic would be simple. when you want it to disAppear make its height 0 and when show make its height back to 8

 if alamatDomisiliSegmentedControl.selectedSegmentIndex == 1 {                   
              heightEmail.constant=8
            print("tidak")

        }else if alamatDomisiliSegmentedControl.selectedSegmentIndex == 0{  
                heightEmail.constant=0 // Assuming you want to hide here
         print("ya")
        }
Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23
  • it works but how to make it the constraint relative not using constant – Indra Sen May 02 '18 at 06:35
  • Sorry did not understand your question.? Can you explain more – Abhirajsinh Thakore May 02 '18 at 07:16
  • oh it works ! i thought if i set the constraint to constant if i use bigger phone the constraint will look mess – Indra Sen May 02 '18 at 07:55
  • you can give condition device wise. If i understood you correctly. You want is that you need to give the constant for each device. In that case just give a simple condition like E.g: if iphone5{ height = 30}elseif iphone 6{ height = 40} – Abhirajsinh Thakore May 02 '18 at 08:32
  • @IndraSen You stated that you are getting the error "Unexpectedly found nil while unwrapping an Optional value". Now you state that this answer works. But this answer would not solve that error. Did you fix that error separately from the change in this answer? – rmaddy May 02 '18 at 14:39
-1

Try this:

if alamatDomisiliSegmentedControl.selectedSegmentIndex == 1 {
heightEmail.isActive = true
heightEmail.constant = 8
print("tidak")
} else if alamatDomisiliSegmentedControl.selectedSegmentIndex == 0 {
    heightEmail.constant = 0 //disappear   
    heightEmail.isActive=false
    print("ya")
}
Andrew
  • 572
  • 6
  • 29