Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout issue for width equally in iOS iPhone

I have three views as subview as shown below with equal width & height constraint

enter image description here

If view B has no data then it should show as below enter image description here

My try If iset B's width as zero then all views width will be zero.

How to get this?

Thank you

like image 926
user2526811 Avatar asked Jun 24 '15 06:06

user2526811


People also ask

How do you add equal width constraints?

To add equal width constraint, you will need to select both views by command clicking on both of them. Then click on the pin menu and the previously grayed out Equal Width checkbox is now available. Click on that and click Add 1 Constraint.

What is aspect ratio constraint iOS?

Aspect ratio constraint is used to control the width and height of a view as per a aspect ratio that you set here. There are some standard presets such as 1:1 which means width will be equal to height. Similarly other presets calculates the dimensions based on a ratio.

What is iOS Autolayout?

Auto Layout is the preferred technology to define layouts for user interfaces on iOS and macOS. Its goal: To make it easy for you to create user interfaces that adapt automatically to different screen sizes and orientations.


1 Answers

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

Now make an outlet and the the constant of with constraint(<2000) to zero.

You can find the sample here.

Why I have set the 2000 is because this constraint is only needed to zero the width. It shouldn't affect the width if the width is non-zero. Now as the width are equally (middle view) and it should be less than 2000 thus the view displays equally on the screen. And when I set the width constraint to zero the width of the middle view becomes zero as it's priority is higher than the equal widths.

enter image description here


This code sets the width of middle view to zero.

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

Output:

enter image description here

like image 138
Inder Kumar Rathore Avatar answered Sep 27 '22 17:09

Inder Kumar Rathore