I have a project I am upgrading from Xcode 4.6.3 to Xcode 6.1.1. I opened it in Xcode 6.1.1, and opened each .xib. The format of each .xib changed as expected. I want to attempt to use auto layout. Use Auto Layout is checked and so is Use Size Classes. I changed every appropriate object from Alignment Frame to be Alignment Rectangle.
For a while I couldn’t add any constraints. Then after some trying I could add a few. See the screenshot.
For the selected View, I cannot add a constraint. The Add New Constraints checkboxes and fields are disabled. Only Update Frames is available to be changed. I can’t add an alignment constraint either. Ctrl-drag a line off the view does not add a constraint either. Notice the View height of 411. If I select another object, then select the View again the height will change to be the height of the parent tab bar less. If I keep doing that the height gets smaller and smaller, then Xcode crashes. How can I add a constraint to this view?
The Tab Bar X, Y, Width, and Height are disabled. When I change the Height of the Assigned View Controller View, the Tab Bar Y changes to that value minus the Tab Bar Height of 49. When I select the View, again it’s Height is now 49 less. Select the Tab Bar, it’s Y is now 49 less. And so on. How do I set the dimensions of the Assigned View Controller View that will stick when I cannot add contraints?
I had the same issue. In my case, the view layout setting was set to Translate Mask Into Constraint. I resolved it by changing it to Automatic in Size Inspector.
Xcode 12, Xcode 13
For UI elements where adding constraints are disabled, check Layout in the Size Inspector:
If Layout is set to Autoresizing Mask change to Inferred.
or
Inferred(Constraints) - The UI element already has one or more constraints.
Inferred(Autoresizing Mask) - The UI element currently has no constraints.
Steps to enable autolayouts:
Select any UI element, in storyboard.
Click on Show the Size Inspector.
Click on dropdown beside Layout option, select Automatic from dropdown.
Zev has the answer. You can't add constraints directly to the top-level view in a view controller.
In regards to the height of the View decreasing when selecting it, I started over from the original .xib making small changes and taking notes. When I checked "Use Auto Layout" and "User Size Classes", got alert the document will no longer be compatible with Xcode 5. Window frame size went from 320 568 to 600 600. ibExternalTranslatesAutoresizingMaskIntoConstraints went from 1 to 0. Other changes apparent as well. Adding constraints to objects contained in View one at a time I am getting good results. The behavior of the View height decreasing when I select it is gone.
Trick to enable constraints on the root view:
Inside the xib, drag a new view that will be sibling to the initial root
view.
Move the initial root view inside the sibling view. It will be
able to have own constraints.
Move the initial view back to be a
root.
Delete the empty sibling view.
Xcode 12 Swift 5
Select the UI Control and click on Show Size Inspector window
Change Layout option to Inferred (Autoresizing Mask))
By default the Xcode 12 keep the setting of layout to Autoresizing Mask, If you want to apply the constraint used Inferred
Related
When using Auto Layout, I am unable to set up a simple UIScrollView in my view controller in Xcode 11 beta 7. I know that I must constrain the scroll view to the edges, and then set the scroll view width and height equal to the width and height of the entire view that contains the scroll view. However, I am not getting the option to set equal widths and heights when I attempt to do so.
When I do the right-click-drag from the scroll view to the entire main view, I get the following options:
Leading Space to Safe Area
Top Space to Safe Area
Trailing Space to Safe Area
Bottom Space to Safe Area
Center Horizontally in Safe Area
Center Vertically in Safe Area
In other videos, there is an "Equal Widths" and "Equal Heights" option that I don't seem to have.
Am I doing something wrong, or did Apple change the way scroll views work in Xcode 11?
I'm using beta 7 currently
I was finally able to successfully add a UIScrollView using storyboards in Xcode Version 11.0 (11A420a) after much trial and error.
You can add the scroll view directly to the root UIView of the VC or, if you needed to, add it to another UIView under the root view as needed by your UI Design. Constrain the UIScrollView parents normally for whatever your needs are.
Here is the key : when you add the UIScrollView to the storyboard, click on the Size Inspector with the UIScrollView selected. You have to UNCHECK the box that says "Content Layout Guides".
If you do this you can set the scroll view's contraints normally (I did 0-0-0-0 to superview). Then I added a UIView to the scrollview and set it's constraints to 0-0-0-0 and a height of 900 (which is what I wanted).
This will give you constraint errors in red when you set them. They will be fixed by setting the width of the UIView equal to the width of the UIScrollView.
Set the UIView width equal to the width of the UIScrollView view by Ctrl-right clicking on the UIView, dragging to the UIScrollView, and selecting the "Equal Widths" option. This should get rid of your constraint errors.
You actually can then go back to the UISCrollView Size Inspector and check the "Content Layout Guides" checkbox, no constraint errors will appear.
Disable the content layout guides in scrollview properties.
Hope it helps.. :)
I have a view controller that's fixed size (it can not be resized). This specific view controller is presented as a sheet in a window, I disabled the resizing by setting preferredContentSize. Since it can't be resized anymore, I left the view without any constraint. However, when I build the app, I got a lot of warnings that says :
Views without any layout constraints may clip their content or overlap other views.
I do know that if you don't add any constraints in a standard window it will clip when it is resized. However, resizing is disabled on my specific window so that I know it won't happen. Is there a way to silence this warning? There are a lot of views on this specific view controller and it's blocking other useful warnings that I may encounter in the issues navigator.
This view controller is presented through a "Sheet" segue.
I had a single warning saying the same thing.
Editor > Resolve Auto Layout Issues > Add Missing Constraints
worked for me.
If it doesn't for you, something else under "Resolve Auto Layout Issues" might work.
For me what worked was the reposition the elements: Drag them again into position and drag their height and width to the desired size again. Then click on "Resolve Auto Layout Issues" and click on "Reset to Suggested Constraints".
Since no one answered this, here is how I fixed it. For each view on the form change the Layout option from Automatic to "Translates Mask Into Contraints". If you want it anchored to the upper left corner, select the top an left red handles and leave the lower and right unchecked.
For me what worked was; select the last element that cause the warning
size inspector tab > layout > Autoresizing Mask
I'm trying to make a divider, so what I do is:
1) Make a new View
2) Change the background to the color I want the divider to be
3) Control click and center it horizontally on a label on top of it
4) Compile, my view is gone.
5) Remove constraints, compile, my view is there.
6) Add left and right constraint for autosize, compile, view is gone.
It also states that the height of my view is ambiguous.
How can the view be gone by merely constraining it?
Screenshot:
Your view does not disappear. It just getting zero size.
When you remove constraints it won't iOS won't layout your view that is why it's size not changing.
And in you last case with leading/trailing constrains your view will calculate its width depending on it's superview width, but since system can't calculate height it gonna change it to zero.
Other views and like labels and images have content and with a help of defined intrinsic content size they system won't resize them to zero sizes but to their content size.
here is my output I tried in swift using Xcode and tried setting constraints as I do
I have an OS X app originally built using Xcode 4, now using Xcode 7. When "springs-and-struts" was superseded by constraints, I reworked the UI to use constraints. Simple enough, and seemed to work well.
Fast forward two years after first release, and for the second release I needed to add controls and increase the height of the main app view. Unfortunately, my test team is using smaller screens and cannot see the whole view. They need to resize vertically.
Problem - even though the resize controls box is checked, the window cannot be resized. The controls do not show at run time. I tried
Setting lower minimum window content size height, but that did not change anything.
Changing content compression resistance did not change anything.
I am thinking this issue has something to do with constraints.... Any ideas on how to get resize to work?
Edit: After playing with a new test app some, I am more certain the problem is due to constraints. I have a control where I have constrained leading and trailing space to superview and width - there went horizontal resize.
I really need to have a view where the user can resize the window, but scroll the content. However, in this case, the content is other controls. I think on iOS, I would use a UIScrollView. On OS X, I have tried a scroll view control and have tried embedding in a scroll view, and neither have the desired effect.
I had the same issue and solved it by adding a view to be used as a "container" in the view controller.
Pin the top left corner of the "container view" to the view controller (leading space 0 and top space 0). Add equal width and height constraints on the "container view" to the view controller. Then move all your objects into the "container view" and add your object constraints on the "container view" not the view controller.
In my case, it happened in this way (Xcode 13.1).
I mistakenly added a view from IB outside of the window view hierarchy. The new view was added as a separated object (a top node in the interface builder file). I added the new view into the window by drag-n-drop.
I found the new view had different behaviours, for example, I couldn't set the top space constraint. With this view in the view hierarchy, I couldn't change the window size (content view size) at all.
I removed the view and added another in the view hierarchy, it worked as normal.
I think IB initialises the view differently if it is a separated object (top node of the interface builder file).
Why I cannot add new alignment constraints on my scroll view ?
In order to add alignment constraints, you must select two or more items before pressing the Align button. This is how Interface Builder knows which two objects to align. You can select the items by dragging a box around them, or you can hold down Shift and select the items.
If you are aligning to centre horizontally/vertically (which doesn't require a second view for reference) and still get this issue, then go to the size inspector (ruler icon in the top right navbar) and change the layout dropdown to Automatic.
This should then enable values and the button in the alignment constraints dialog.