Auto detect problem for width the same in iOS iPhone

I have three subviews as shown below with equal width and height restriction

enter image description here

If view B has no data, it should be displayed below. enter image description here

My attempt . If iset B width is zero, then the entire width of the views will be zero.

How to get it?

thanks

+6
source share
4 answers

What you can do is add a width constraint to it (average) and make less than 2000 with priority (1000) more than equal width (750)

Now make an exit and a constant with a constraint (<2000) equal to zero.

Here you can find here .

Why I installed 2000 because this restriction is only necessary for zero width. It should not affect the width if the width is not equal to zero. Now that the width is the same (middle view), it should be less than 2000 , so the view is displayed the same on the screen. And when I set the width limit to zero, the width of the middle view becomes zero, as the priority is higher than equal widths.

enter image description here


This code sets the middle view width to zero.

 - (IBAction)btnClick:(id)sender { self.widthView2.constant = 0; } 

Output:

enter image description here

+3
source

First make sure your width limits are equal:

 A.width == B.width A.width == C.width 

And not :

 A.width == B.width C.width == B.width 

That is, make sure that the representation B is involved in only one constraint of equal width.

Then create output points connected to view B and to an equal width constraint on view B.

 @property (nonatomic, strong) IBOutlet UIView *viewB; @property (nonatomic, strong) IBOutlet NSLayoutConstraint *bEqualWidthConstraint; 

To connect a limit output, you need to find the limit in the storyboard layout. This will be one of the restrictions for the closest common ancestor A and B; it won’t be directly on A or B. When you find it in the storyboard path, you can control the drag and drop from it to the socket in the source code (or you can create a new socket by dragging the controls anywhere on your @interface ).

Add another property to set the other B constraint to zero:

 @property (nonatomic, strong) NSLayoutConstraint *bZeroWidthConstraint; 

Initialize bZeroWidthConstraint , but do not set it:

 - (void)viewDidLoad { [super viewDidLoad]; self.bZeroWidthConstraint = [NSLayoutConstraint constraintWithItem:self.viewB attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:0]; } 

If you want to hide view B, remove bEqualWidthConstraint and set bZeroWidthConstraint .

In iOS 8.0, you can remove a restriction by setting its active property to NO , but in earlier versions you need to send a message to the view on which the restriction is set. This is usually the closest common ancestor of limited views. Assuming the common ancestor is A and B viewB.superview :

 - (void)hideB { [self.viewB.superview removeConstraint:self.bEqualWidthConstraint]; [self.viewB addConstraint:self.bZeroWidthConstraint]; } 

Note that since bZeroWidthConstraint restricts viewing to B only, you can set it directly to view B.

If you want to display view B again, remove bZeroWidthConstraint and reinstall bEqualWidthConstraint :

 - (void)showB { [self.viewB removeConstraint:self.bZeroWidthConstraint]; [self.viewB.superview addConstraint:self.bEqualWidthConstraint]; } 
+2
source

First, you need to understand that you can prioritize different restrictions in iOS. The default priority is 1000, which is the highest.

Now you need to set equalWidth to 750 priority. Second, you need to apply the horizontal distance from ViewA to ViewB and ViewB to ViewC. Ideally, the constant for the horizontal interval should be 0, which will make the sides of the three types stick together.

And now, when you make the width for ViewB equal to 0, your ViewA and ViewC will occupy all the space with the same width, and the ViewB will disappear.

Please let me know if you have any confusion about this.

+1
source

Download code: set limits according to your requirements. Click to download

create the variable NSLayoutConstraint _distance_ViewA_ViewC for the distance between ViewA to ViewC

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //when viewB is hidden remove the comments _distance_ViewA_ViewC.constant = 8.0f; _viewB.hidden = true; } 

audio output

For viewA, viewB, viewC


enter image description here

enter image description here

viewA, ViewC for viewA, ViewC and viewB

enter image description hereenter image description here

0
source

Source: https://habr.com/ru/post/989653/


All Articles