How to set dynamic limitation for buttons in lens c

I have a simple UIView that has two buttons named "Button1" and "Button2". Under certain conditions (assuming an X condition), I need to show both buttons as shown in Figure 1.

In another condition (suppose condition Y), I need to hide "Button2" and also place it in the center of the UIView, as shown in Figure 2.

If you could provide me with an example it will be very helpful for me. I need to do this in an iPhone only lens.

thank

enter image description here enter image description here

+3


source to share


2 answers


I did with 3 buttons

You need to specify the following restrictions.

  • btn1 leading to his supervisor

    btn2, resulting in btn1 termination

    btn3, resulting in btn2 termination

    btn3 going to his supervisor

    btn1 equal width to btn2 -> Priority 999

    btn2 equal width to btn3 -> Priority 999

    btn1 top for your supervisor -> Priority 999

    btn2 top for your supervisor

    btn3 top for your supervisor

    btn1 width Constraint -> Priority: 750, constant -> 0 Take IBOutlet



for all 3 buttons you also need to set height limits.

After that, when condition Y Occurs

Set btn1 width Limit priority: 1000 And you are done

0


source


To do this, you need to have two sets of constraints, one for each layout (one button and two buttons). You switch between these sets when the view needs to change.

If you want to switch, you want to deactivate the first and activate the second. You do this with deactivateConstraints:

and activateConstraints:

. For a single button layout, you'll also want to remove the second button from the view by calling on it removeFromSuperview

(and re-adding it when you switch back showing two).



If you are using Interface Builder you need to create IBOutlet

for the constraints created there so you can access them.You will probably also want to create only one set of constraints in the Interface Builder as they will conflict with each other if they are active at the same time.

If you are not familiar with how to create constraints in your code, Programmatically Creating Constraints can help you .

0


source







All Articles