Xcode missing constrains although app not using size classes? - ios

I have an older app which is not using size classes and I will not use constrains in this app. I did some changes on the layout (using xcode 7.2) and since than I am getting some errors "missing constraints" although I am NOT using size classes? And it is true from time to time the layout is wrong (but not ever?). How can I turn off constrains for this screen?

If you want to let xcode set constraints then you can select reset to suggested constraints as follows..

Related

Xcode 7 to Xcode 8 Storyboard Constraint Warnings

I just converted my Xcode project from Xcode 7.3 to Xcode 8 GM and I am getting over 80 constraint warnings. In Xcode 7, my storyboards were "big squares" rather than a specific iPhone size. Now, with Xcode 8, you can change to the different iPhone and iPad sizes. My constraints are still showing up as they were with the more squared-off VCs in Xcode 7. Is there a "quick" fix or do I need to go through my entire app and fix each and every constraint (far more than 80)?
UPDATE: September 20,2016 -> Many bug reports have been filed based on this feed and Apple should be aware of the problem. I will update with an answer as soon as the problem is fixed.
Select all the Scenes(View Controllers) in the Storyboard, show the Size inspector, change the Simulated Size to Freeform, then set Width Height = 600.
Then, those tons of warnings disappear.
I had the same issue when updating to Xcode 8. My storyboard looked just like you describe. Check your warnings/errors. If they all say something like "set vertical hugging priority to X" or "set compression resistance to Y" then you should be able to fix your issue by setting the priorities as suggested.
Click a warning, then in the attribute inspector under the tab with the ruler icon (not sure what that tab is called) you can set the priorities under the Content Hugging Priority and Content Compression Resistance Priority headers. As you update these properties the warnings should start to change to incorrect frame warnings, at which point you can just update the frames.
This might not be a quick fix, but changing a few priorities for some constraints is easier than redoing all the constraints. I hope this helps!
Select the view controllers one by one and press "Update Frames" each time.

Alignment issue when running simulator

I am trying to run a simple sample using a nib file and it looks like this
However when I run it in my simulator it looks like this
My question is why is the alignment of the simulator not correct ?
The elements should be close to the middle and the screen height should be longer. Any suggestions on how I could fix this ? I am using Xcode 6.2
Here are the things I tried so far :
1 ) I made sure that the alignment in my nib file is set to Auto Layout.
These are the details of my project. I am testing on an iphone 6 as my simulator.
Any suggestions on how I can fix this ?
You need to set Constrain in AutoLayout if u don't want to use auto-layout then u can unCheck it .. you can refer this tutorial http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1
You probably need to configure size classes for the class device that you use.
If your app is only for iphone, you can also disable size and keep size classes for iphone only.
Hint Use preview from: Assistant editor -> Automatic > Preview, you will be able to see a preview of the view with all device screen sizes

Automatic constraints to storyboard and xib in XCode 6?

I was just trying to learn Autolayout in XCode 6. But I noticed a strange change in the XCode 6 that is whenever I drag and drop a button (or any UI element) on a xib the XCode isn't automatically adding the constraints to the button. However I do remember this facility was there in the previous versions of XCode. But in XCode 6 I had to explicitly add the constraints to the UI element. Here are the images of what happens in previous version of Xcode 6 and previous version of Xcode .
There we can see in XCode 6 no constraints have been added by default but however in previous version of XCode constraint was added by default.
So it would be helpful if you guys let me know if there's a way to enable the feature of adding constraints by default in XCode 6 or is it the way XCode 6 ways where we need to add constraints manually?
Thanks in advance :)
With the introduction of size classes, to have a universal storyboard for both iPhone and iPad, setting the constraints automatically wouldn't make much sense for me.
You can ask Xcode to resolve auto layout issues for you by pressing the (Resolve Auto Layout Issues) button in the lower right of the Interface Builder window and choosing “Add Missing Constraints”. The added constraints will fix the subviews relative to their superviews.
Adding the constraints automatically while editing was done in Xcode 4, and it was a disaster. Every time you moved a view, it broke something.
As of 5, any missing constraints will be added at compile time to your views. If you select a view and look in the size inspector, it will tell you this. See here for more details on constraints and interface builder.

In xcode 6.1, interface builder deleting ui elements

I have a view with some tableviews and some collection views and also some buttons and labels.
I'm not sure if this is new with Xcode 6.1 because I have not worked on the ui aspect of this project for some time. However, when I try to resize just about anything in the view, my tableviews and collection views, and possibly some other elements I am not noticing, get deleted. Specifically, it seems their frame or rect propertied get deleted.
To be clear, this only occurs when I attempt to resize the element by dragging the sides in interface builder. If I resize them by changing the numbers directly either in the IB sidebar or in the xml, this does not happen.
I'm wondering if this is a bug in Xcode or if I have done something to cause this behavior which I can change.
Thanks!
I acheived a fix, at least for this project by turning off auto layout in "show the file inspector" -> "interface builder document"
I have never used this feature in the past so I do not know if it was on in the past but I am wondering if the new Xcode version turns it on by default where the others left it off.
It may also be possible that it has always been on but there is a bug in the feature in the new version.
This fixed the issue for me, at least in this particular project and at least for now. I hope others find this useful.
From here it looks like a bug in Xcode 6.1.
I created a clean project and the behaviour is the same on my machine:
https://github.com/nasht/Xcode6ConstraintTest.git
Resizing one view makes others disappear. Changing a layout constraint from a = to <= or >= also seems to do it.
I've raised a bug with Apple. Suggest you do the same.
workaround: You can install xcode 6.01 and use interface builder from there. You can still compile and run your code using xcode 6.1 if you're relying on xcode 6.1 specific features. (you'll need to rename your xcode.app so you can run both instances. ) It's ugly, but it works.
I found that it's fixed on the next version of xcode (6.1.1)
same problem, yosemit and xocde 6.1. I can resize some view only if i disable autolayout.
I think I ran into this 'problem'. I believe this was because one of the constraints I had was not setup correctly.
What OS X Version are you on? I updated to Yosemite today and 6.1 along with it. When I would resize a label my other elements were not getting deleted but instead getting resized and repositioned. The width / height would change to 0,0 and the x / y would change to 0,0 as well.
I was noticing other issues with Yosemite so went ahead and did a time capsule restore back to Mavericks and Xcode 6.0
I had the same problem in universal app; iphone works fine and the ipad doesnt work.
I'm using xCode 6.1 and OSX Yosemite
My app supports iOS7 and iOS8 and i had the same problem on iOS7 on iPad version only
Fix:
So i fixed it by setting Simulated Metrics in interface builder by
Size = iPad Full Screen and Orientation by Landscape or Portrait
I have got the same kind of problem with Xcode 6.1 and Yosemite :
I have created a view that I have added to the main view, so it appears like a sub view in the View Controller Scene.
Then I resize this subview to 320 x 568. My program works. It corresponds to the program dropit of Stanford CS193P Lecture 9.
I save the project, close it and reopen it : the subview has its width and height set to 0 !
I tried to open the project dropit of Lecture 8 that is quite the same, which was working before (I think in Xcode 6) and same problem ! First I thought that tapping (related to the subview) was not working but found out that tapping was not working because I was tapping on a subview which has its width and height set to 0 !
I have reset the size class wCompact hAny to which all the UIElements are added. and got corrected.

Layout attributes relative to the layout margin on iOS versions prior to 8.0

What would be causing the following warning (and subsequent alignment issues on iOS 7)?
Attribute Unavailable: Layout attributes relative to the layout margin on iOS versions prior to 8.0
None of the posted answers solved the problem for me. But the reason for this is the following: Xcode 6 creates constraints based on relative margins by default. Those are only available on iOS 8.0 and newer. You get these warnings when your deployment target is set to iOS 7.0 or lower.
The way I fixed the warning:
Click the warning in Xcode
Attribute inspector will open the constraint
Search for item that has margin (see screenshot)
Turn off Relative to margin option
Disable "Prefer margin relative"
Unchecking the "Prefer Margin Relative" checkbox will keep you from getting into the situation where you get this warning.
If you are like me and you built an entire app with constraints before realizing the problem, then things are a bit tougher because Xcode will not easily tell you which constraints are a problem.
In order to avoid rebuilding all of my constraints, I resorted to looking at the actual .storyboard file and I looked for constraints like this:
<constraint firstItem="vId-..." firstAttribute="top" secondItem="In7-..." secondAttribute="topMargin" id="C0H-..."/>
Notice "topMargin". The attributes ending in "Margin" cause the warning. I went through and identified the constraints in the file this way, then I removed and re-created them in IB. After that, this warning went away.
I suspect this should also resolve some inconsistencies between iOS 7 and iOS 8 constraint handling, although I am still encountering some differences in behavior, even after addressing all Xcode warnings.
I'm currently in the same situation as I'm creating an app in Xcode 6 that targets iOS7 and 8. That warning appears because the constraints relative to the margin aren't available in iOS7, but they are created by default in Xcode 6.
You can absolutely go back and edit the constraint to not use that 'Relative to margin' feature as suggested by the other answers to this question.
When creating new constraints, I hold down the Option key when choosing which kind of constraint to create. This gives me the ability to create a constraint that's not based on the margin right away so I don't have to go back and fix it afterwards.
Xcode 6 creates constraints based on relative margins by default.
So, if you want to force remove all baselines attributes and don't want to search all bad constrains in Interface Buidler, to support iOS 7, I can recommend this way
To find and remove all dummy strings with "Baseline" in constraints you can do this:
Close Xcode
Open your stroyboard file in your favorite text editor, that supports regexp.
Find and remove from storyboard file all strings by pattern: .*"baseline".*\n
Now save file and open it in Xcode
Fix all appeared misaligns: find all warnings and press "Update constraints for all views" to save original position of all views.
Profit!
UPD:
I found that "baseline" constraints causes crashes also, but Xcode doesn't show any warnings about these constrains!
To fix it - remove from storyboard file all strings by pattern: .*"baseline".*\n
I took a non programmer approach.
I knew which view controler was causing the 8.0 margin message. So, I went to my constraint list. I had 33. I've remove every one that was causing the 8.0 margin message. I went down to 20 (So 11 was problem).
Select 32 over 33 delete and see the result. Select 31 over 33 delete and so on ... Naturally when the message still there ... delete the problematic one.
So it took me 5 minutes to resolve the error message.
Other constraint messages did appear but that's not a big deal.
Ok than, redo and resolve step by step the constraint messages but this time check that all the constraint you add don't provoke the reappearing of the 8.0 margin message. If so, undo and take an other strategy when adding constraints. There's always an other way to add a constraint that work.
That's not what is proposed by Xcode but it will do the job.
At the end, your done.
If you have more than one view controller, and don't know witch one is causing the 8.0 margin message, try to isolate each view controller. May be in a separate test project with a copy and paste or deleting all the other view except one and se the result.
Using the "Create Snapshot" in the file menu can help you if something goes wrong.
Suppressing the warnings: I went through all IB constraints and deleted all that said "First Baseline...". This made all of these warnings go away. However, IB will reintroduce the warnings if you "Add missing constraints" or "reset to suggested constraints".
Possible cause: From a fresh Xcode 6.1 project, I copy/pasted ViewControllers in Storyboard (lazily), using "reset to suggested constraints". Then changed the project target from 8.0 to 7.1. This caused the warnings to pop up. I noticed that the warnings only occurred on certain of my ViewControllers, which I believe were the ones I copied/pasted in Storyboard.
Suggestion: If targeting 7.1, set it up front before using IB. Don't copy/paste view controllers in Storyboard. And be wary of "new warnings" when working within IB, so hopefully we can confirm the cause of this annoyance.

Resources