I am working through the CS193P Stanford iOS course and I am struggling with auto layout. I have laid out my calculator ViewController and set up all of my constraints. Now I need to add additional UI elements and I'm not sure how to got about it. In the storyboard editor in Xcode my existing UI elements are taking up the entire view controller. Do I need to just delete all of my existing constraints, move the UI elements around to their new positions and recreate all of the constraints or can this be done incrementally without having to recreate all of the constraints.
You can move and resize the controls with the constraints still in place. Xcode will note that there are misplaced views, but that will go away after you adjust the constraints.
Depending on exactly what you want to do, you can tell Xcode to adjust the constraints to match the new layout on the canvas. However, if you need to interpose a new control between things that were previously spaced relative to each other, you should delete that constraint and add two new ones. That is, if you had something like (in visual format language) [view1]-[view2] and you want to put another view between those to get [view1]-[view3]-[view2], you will want to remove the constraint between view1 and view2 and create two new constraints, one between view1 and view3 and another between view3 and view2.
Related
firstly I have Placed 3 labels in the xib file and I added constraints to the elements(using the Auto layout in storyboard), next I got the requirement to add an image to its background so to do this first I need to remove those constraints after that I added UIImageView and finally need to set constraints to all UI elements but if I have more UI elements with more constraints its more difficult to remove constraint and adding view how to solve this issue
Sometimes its hard to implement when information is poor when you begin. To solve this particular problem, you could have a transparent container view. Just make UIView, place your Elements on it and add constraints. When you now have to add another view behind it, you just have to remove the constraints from the container. Makes things easier.
Another thing is, you can leave those constraints as they are. They refer to the view you have put the Elements in. When you now add another view behind them, you just can add constraints to this new view and the other elements are not affected.
Hope that halps a bit!
I´m pretty new to Xcode and playing around with some techniques.
For testing, i am writing an app where i have a detailViewController with three views within.
As soon as i assign constraints to one of the views the layout gets really strange. No matter if i center horizontally or set border distance, the view seems somehow to vanish and the components within the view are placed somehow.
Any idea what i´m doing wrong?
Sure, you need to follow these basic rules. Checkout the auto layout guide. Each view from the top of the scene to the bottom should have constraints (also constraints should exist from side-to-side if you want expansion and contraction, or have height/width if those are important to maintain). Most times you will need to have at least four constraints per view. The auto layout errors will not go away until all the views have constraints applied and adhere to the simple rules I mentioned above.
I have been learning Xcode and iOS and things are going well with coding but I am confused about a few options in interface builder.
It seems that when I add a contraint I can choose to "Update Frames" and then either "Items of new contraints" or "All Frames" but it seems that I don't always need to do this... sometimes I do and sometimes I don't - I don't think I truly understand the concept. It seems to be optional. When is the case to choose it or not?
Once a constraint is added, then should that be it?
There is also a context menu (Resolve autolayout issues) (3rd item on the bottom on interface builder, represented by a triangle between 2 vertical lines) which also allows me to Force Update of constraint or update of frame. Why is this needed ? I notice the options inside are usually disabled so I presume this becomes active when there are issues ?
I have tried searching the internet and I can't find anything specific; the more I read, the more confused I seem to get.
Also, a sanity check: a frame is the bounding box of every single control (view) not just the viewcontrol, so every view (control) as its own frame. Is this correct?
I'll try to explain the concept of constraints and storyboard representation.
Constraints - are used to determine the position of UIView elements inside your view. For example, the vertical distance from top of the view to UIView element top. When you add constraint using the storyboard it plays 2 roles:
Positioning the element inside a view when your app runs
Positioning the element in the storyboard representation itself
Update Frames - is used when you want to update storyboard display so that it fits the currently applied constraints. When the display of the storyboard does not match the constraints a warning will appear.
Add missing constraints / Update constraints - will create/change the constraints applied on UIView element to match what currently presented in the storyboard. Kind of the other way around from update frames.
This answer is not a replacement for Apple's Auto Layout Guide
You do not have to click on "update frame" every time you add a constraint, if you do, it will update the frame of the view to whatever it should be based on the constraints. Yes, every view has a frame and it is the rectangle relative to the views superview.
More often than not you'll probably click on update constraints (Shift + Command + =), this will allow you to resize views that are already constrained, and have the constraints updated to match.
in an ios application,
I have a custom UITableViewCell with a lot of views in it. I am using auto layout and setup all the constraints as I need.
But now I need to have a main view (in the content view) and put all the other views in it (some sort of a parent view to all).
This is an example of how my cell looks in the interface builder:
it has all the views setup with the constraints.
I know there is an xcode feature to embed selected views inside a parent view:
So I select all the view and select to embed them in a view. I get the following:
My Problem
Although the views are embedded correctly in a superview, the autolayout constraint were removed, and now I have to recreate them.
Is there a way I can put all the views inside a superview and have the same autolayout constraints without having to recreate them all?
Thank you
As mentioned in the comments above, this approach works well for me:
indiestack.com/2013/12/transplanting-constraints
In short, the approach is:
Give your old superview a restorationId so you can easily identify it in the XML.
Open the XIB/Storyboard you are working with in a text editor view.
Copy and paste what is currently there into a backup file.
Embed your content into a new superview using the storyboard. Do not set any new constraints yet.
Give your new superview a restoration ID so you can easily identify it in the XML.
Open up the current storyboard/XIB side by side with the old storyboard/XIB you backed up.
Find the old container view in the backed up XML by searching for the restoration ID. Copy everything inside the <constraints></constraints> and <subviews></subviews> tags
Find the new container view in the current XML and replace the content of its <constraints></constraints> and <subviews></subviews> tags with the copied content
Go into the old XML and copy the ID of the old container.
Go into the new XML, and replace the old ID you got from step 9 with the id of the new container everywhere inside the <constraints></constraints> you pasted in.
Don't forget to add constraints to your new container that match the old container (in my case this was pinning the new container to all the edges of the super view).
Caveat:
This will break any outlets to constraints you may have set up. Outlets to other things, views, etc... will be fine.
Select all views
goto editor -> embed in -> view
Again select all views
Add new constraint , pin top, bottom, leading, trailing to 0
Have you ever tried to just create the constraints of the container view? You don't need to recreate all the constraints.
They are appearing as incorrect because the loosing they parent constraints.
Create a new view at the top level of the xib. Then drag your existing view into it. Existing constraints will still be there. You'll just have to set new constraints up for the embedding.
This seems like it should be very simple, but I am having a terrible time trying to get every subview (they act like menu bars) constrained to the bottom of the parent view in the IOS storyboard.
There seems to be automatic constraints placed between the subviews that make them override the constraint I'm trying to set. I try to take these off, and then place the new constraints, but nothing seem to work as I loop over trying to reset constraints, over and over and over...
It should be very simple, set all of the uiviews to a particular width and length and set the distance between the bottom of the superview to be equal to 0. That's it.
The secret to happy constraint editing in interface builder is simple:
Don't do it
If that isn't possible, then:
Create your own constraints before trying to delete the system ones
Make judicious use of the pinning menu and editing constraints in the inspector
Don't drag and drop your views after you've added them - this causes interface builder to discard all constraints and set you up with a new set of "best guess" system constraints.
I've written more about this here but that's the gist of it.