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.
Related
Assume the following, simple layout:
Three views vertically stacked upon each other
Using simple vertical spacings between the views
Is it possible to hide the red view including its margins using constraints / AutoLayout only?
Settings redView.isHidden = true will hide the red view but will not change the position of the blue view. The blue view will stay at the same position as if the red view would be visible.
Using redView.removeFromSuperview() to completely remove the red view would show the desired result. Due to its optional spacing constraint to the gree view the blue view would move to where red view was. However it would be quite hard to re-show the red view because all its constraints would have to be set up from scratch.
In Android setting the visibility to View.INVISIBLE simply hides a view (as the first case described here) while View.GONE renders the remaining layout as if the view was not there at all.
Can this be done with iOS using constraints / AutoLayout only?
Of course I can achieve the same buy manually manipulating the constraints and setting up new constraints in code. But the question is, if there is a more convenient solution as in Android?
A VerticalStackView seems to fit your requirements. You can include all the views in the stack view and set the spacing directly on it.
Then, is one of the views is hidden, the stack view will automatically adjust all the constraints.
Take a look at the pictures:
If possible, wrap your views in a vertical UIStackView. You can then individual views and the other views will be rearranged as intended. You also don't need to add constraints between items, since the stackview handles the spacing between views.
The simplest way is to embed the views into a StackView and when one of them is hidden, the one below will move up into its place.
Follow these steps:
Add all the views you need in the storyboard/xib
Editor - Embed in stackView
Set the spacing in the stackView
Set the stackView constraints
Create outlets for the views you want to be hidden in a certain case
Set that views hidden property to true
Regarding the constraints, you can set them for the StackView and for the vertical one, just set the equal spacing and the space properties in the StackView.
Result:
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.
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.
Am new to Auto layout constraints in Xcode. I have one Bottom View like UITabBar with 6 UIButtons. Without constraints I aligned those buttons with 5 space padding with each buttons and each buttons has 50 width.
Now,I am trying to make it by using Auto layout to support all Screen sizes.
In Storyboard constrains I set equal width for 6 buttons and I made 60
space between each buttons. I set first button 0 to leading space and
I set the last button 0 to tail constraints.
I want the buttons with equal width and flexible spaces between views in all device sizes.
Can anyone pleas help me? Looking for the help. Thanks in advance.
check this image and made your constraints like below...
RESULT:- preview in different sizes
With Stackview (For iOS 9.0 and above)
NOTE: If you have to make app for iOS 9 and later then UIStackView is another option for you
With iOS 9 and Xcode 7, you can now have the expected result with UIStackView and only 3 or 4 auto layout constraints.
The following step by step uses Storyboard.
Create 6 UIButtons and 5 UIViews. Align them horizontally. Give some color to your views.
Select all your views and buttons and click on the Stack button. You will have all your buttons and colored views aligned in a UIStackView.
Select your stack view, go to the Utilities panel and select the Attributes Inspector item. There, click on the Distribution button and select "Fill Equally".
For now, your stack view's height relies on your buttons intrinsic content size. If you're fine with it, you can go to step 5. However, if needed, you can give your stack view a height constraint. Select your stack view, click on the Pin button, check the Height button, add the required value and click on the Add 1 Constraint button.
As an alternative, If you want each element of your stack view to have its height matching its width, select the first button of your stack view, click on the Pin button, select Aspect Ratio and click on the Add 1 Constraint button.
You can check that your aspect ratio constraint is correct in your Document outline (left panel) and, if you need, you can change it with a different constraint in the Attribute inspector (right panel).
Now, it's time to give some external constraints to your stack view. Select your stack view. Click on the pin button, make sure the Constrain to margin button is not select and set leading, trailing and bottom constraints to zero. Make sure that the bottom constraint is related to your view controller's view. Then, change the Update Frames button to "All Frames in Container". You can now click on the Add 3 Constraints button.
Your stack view is now set.
Further remark:
If you don't need your colored views width to match your buttons width, you can build a stack view with only UIButtons and simply add spacing to your stack view in the Attribute inspector. However, you will have to find a way to add a background color behind your stack view. Apple states about it in the UIKit Framework Reference:
The UIStackView is a nonrendering subclass of UIView. It does not
provide any user interface of its own. Instead, it just manages the
position and size of its arranged views. As a result, some properties
(like backgroundColor) have no affect on the stack view.
I've build a Xcode project with 4 different stack views:
one with colored views and relying on the embedded UIButtons intrinsic content size for its height,
one with colored views and with a UIButton "equal width and height" constraint,
one with colored views and with its own height constraint,
one without colored views but with spacing and embedded inside a colored view.
You can find this project on this GitHub repo.
iOS 9 has a new UIKit class called UIStackView. It is very helpful in stacking up views horizontally or vertically, like the way you want. You should check out this tutorial: Raywenderlich: UIStackView Tutorial
Select all the buttons and in Xcode’s Pin menu
Create a horizontal constraint from the top left view to the top right view by selecting the red line to the selected view’s right side nearest neighbor
Create a horizontal constraint from the top right view to the top left view by selecting the red line to the selected view’s left side nearest neighbor
Create a vertical constraint from the top view by selecting the red line to the selected view’s top side nearest neighbor
Select checkbox near to Height, Equal Widths and click on "Add 22 Constraints"
Spacer views should no longer be necessary unless you need space between borders.
You can create a StackView with equal spacing which will dynamically adjust the space between your stack views to use up all available space without altering the size of your subviews.
Interface builder can be a bit misleading here. the value you put in for spacing is the minimum spacing required not what the actual spacing will be.
The StackView will handle your spacing, making the size of your buttons equal you can use regular constraints for.
I created a scroll view in Xcode that works awesome because of this video.
https://www.youtube.com/watch?v=3PIm8-lKAYw
When I was messing around after I made it I found out that if I clicked on the scroll view and went to Show the Size Inspector or the fifth button on the right hand side of the screen I had the option to make a constraint called Top Space change in value that caused the scroll view to become bigger and smaller. I decided to see if I could find a way to change the constraint programmatically by simply using dot notation and the equaling it to an int value that I wanted.
So what I'm trying to figure out is there a simple way to change these constraints values programmatically that change the scroll view constraints values?
Without actually following that tutorial (and there being no code in your question) I'm going to make a few assumptions.
Yes, it is possible to change the scroll view's content size by manipulating constraints in your code. If you are creating the constraints in a xib or storyboard, you will need to make sure they are hooked up to IBOutlets so that you can access them in your code.
If you have 2 views arranged vertically that affect the vertical content size of your scroll view, increasing the space between these views would also increase the vertical size of your scrollview's content size. The following would increase the space between 2 views by 20 (assuming a multiplier of 1) and subsequently increase the scrollview's vertical space by the same amount.
// This is a vertical space constraint created in your xib or storyboard between 2 views that drive the content size of your scrollview
someVerticalSpaceConstraint.constant += 20;