iOS Making Two Views Cover Full View - ios

I am trying to make my two sub views cover the width of the main view. Currently Day One becomes wider and Day Two stays the same and does not become wider. They both have the same layout constraints so I don't see the issue. Any solution to this?

Remove width constraint of view one and make Equal-width constraint between the 2 views by control-drag from view1 to view2 and select Equal-widths

Add one more constraint that will tell them to have the same width.
Programmatically:
dayOneView.widthAnchor.constraint(equalTo: dayTwoView.widthAnchor).isActive = true
In storyboards control drag from Day One View to Day Two View and select Equal Widths.
Without this the autolayout has no chance to know that you want those two views to have the same size.
Also, I think when you add it, you won't need the constraint width >= 154 on those views.

Related

How to constraint from the center in Xcode

I'm new to Auto Layouts and the concept of constraints. I'm currently trying to create a single view app with two buttons:
The buttons must meet the criteria shown in the image above. I can do everything except the two constraints where the buttons have to be a 30 above and 30 below, as it resizes the entire button to the point that one overlaps the other.
Is there any way to achieve this? I'm not doing the constraints programmatically, but rather on the storyboard. Many thanks.
Try put both of them in a view, add constraint between them 60px height, then make the view center of the superview, leading and trailing 20px, optional 50 from top and 150 from bottom, you should get the desired result.
The main issue here is to ensure that after setting alignment constraints both from the center, to select the following option of either the "bottom" or the "top" depending on how you want it:
This ensures that there is no "overlapping" caused.

Adding Size Constraints for Views inside a UIStackView

I currently have a UIStackView that I add views to, it fits a maximum of 10 views along with a button. The issue however, is that although the views I add are equal size, the button is not.
I have tried adding a constraint that determines sets the button's equal to 1/11th of the StackViewSize (so that all of the views can fit equally), but it just seems to break all the other constraints.
How can I give the button size without breaking a million constraints?
Currently the height of the button is adjusted with each new view added, but that's just ugly.
First of all, set height and width to that button according to your requirement using AutoLayout. Then select your stack view from the Document Outline(in your view controllers hierarchy) & then in Attribute Inspector set its Distribution to 'Fill Proportionally'. Add some spacing if you want.

How we can increase width of two button when we remove third button from view

I have view with three button with equal size. Each button take 1/3 part portion of view.
Like this image:
If I remove/hide one button then two button width should increase equally and take 1/2 portion of view. if I remove two button then one button size should be equal size of view.
My question is, how it's possible using the Autolayout.
Best option is using stackView. StackView gives lots of flexibility in adding or removing items. If you wish to use only auto layouts, you can achieve it by connect it's width constraints as IBOutlet and change the values programatically.
Best way to do that is to use UISTACKVIEW.Place a stackview and add 3 buttons.You can give proper layout constraints to the stack view as you need
click on stack view-- select attribute inspector
change distribution--fill equally
spacing--0
Then after that if you hide any button,other buttons will be automatically adjusted in width
Other Possible sol to this problem is Adding or removing constraints during runtime is a heavyweight operation that can affect performance. However, there is a simpler alternative.
For the view you wish to hide, set up a width constraint. Constrain the other views with a leading horizontal gap to that view.
To hide, update the .constant of the width constraint to 0.f. The other views will automatically move left to assume position. and for equal width pervoid multiplier to width..
You have a few options:
UIStackView which was made exactly for this.
UICollectionView similar to UIStackView in a certain way, but not really meant for this. However, it does the job nicely and it's easy to implement. Sometimes easier than UIStackView.
NSLayoutConstraint by using multiple constraints with different priority so that you can activate/deactivate them as needed and get the desired result. This approach is a bit more complex by it gives you the highest degree of control and flexibility over the views in your hierarchy.
The best way to achieve what you are looking for is, like others have already mentioned, to use a UIStackView.
When the isHidden property of a UIView inside a stack view is set to true, that stack view will hide the view and take care of the layout, so you will only need to set the correct constraints for your stack view.

How to set space between two view using auto layout(in Aspect Ratio)

I have two views in a view and i have a requirement to increase the distance between those views in aspect ratio to the main view. I am using auto layout.
Thanks in advance.
One way to solve your problem is -
For your upper view give top space constraint with super view and other required constraint.
For your lower view give bottom space constraint with super view and other required constraints.
You don't really need to give vertical space because your views will be pinned with fixed space with top and bottom of super view, so if screen size will grow space between your views will automatically grow.
Important to note however: You must add the height constraint for both views and have either fixed width constraint or have Leading and trailing space constraints.
See ScreenShot
Since you have more than one view, I recommend doing the following assuming this is the result you are looking for:
STEP 1 :
select all your views
Step 2 :
go to Editor -> Embed In -> Stack View
(now it will group them together) I'm assuming that you set the width and height constraint for each of your views before doing this.
Step 3 :
once you have done that select the stack (not the individual views). but rather the entire stack (I would do this from the side menu) and set the top layout and the bottom layout.
step 4 :
Go here and make these settings for the stack as you have it selected
and that should do it.
Let me know if I should clarify anything.
UPDATE:
If you do not want equal spacing, you can still use the above method by playing around with different stacks, etc.. However, There is a method I've used in the past to get this, which is setting a multiplier instead of a number for say spacing between bottom layout and view. Here is an example that might help below:
you can play around with it to get your desired results.

iOS - Storyboard - modify UIView that is out of frame with autolayout

It's a fairly stupid question but here we go:
I am using Autolayout right now to create a UIScrollview with 3 different UIViews I am scrolling through. I all created it using Storyboard and it works perfectly -- the first View is in the middle of the frame, and the 2 other ones are horizontally out of the frame. When I scroll in the Simulator it works perfectly.
All my frames have proportional widths to the main Superview, which means that they take the whole screen's space.
My issue is that I can't access the two other views to add elements to them through the Storyboard because they are not in the frame -- even if I change the ViewController's size, all the Views just get bigger because their size is proportional to the main view's size.
I would like to add elements to the right side of my second UIView (the third one is even more to the right), but you can't drag n drop objects if it's not on the ViewController's frame.
How could I solve this issue without having to mess up all my constraints?
Thanks a lot !
One way to deal with this is go to your storyboard -> select your firstView -> View Constraints and edit the 'Leading Constraint to Superview' constraint negative multiples of width of the screen like -320, -640. It will then show you the second and third view respectively.
Image attached below.
Note: You need to remember to set this back to 0 once you are done editing.
You can drag new views into the view hierarchy shown on the left, and use the same to select any items to edit them, add constraints, etc.

Resources