67

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

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Andy
  • 14,260
  • 4
  • 43
  • 56

7 Answers7

92

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

Attribute inspector

Legoless
  • 10,942
  • 7
  • 48
  • 68
  • Why would I get these if Auto-layout is turned off? – ICL1901 Aug 22 '14 at 18:53
  • 2
    @RubberDuck There is. – Matthew Quiros Sep 12 '14 at 03:26
  • Restart XCode 6 of the attributes inspector is not opening – railwayparade Sep 23 '14 at 01:35
  • 1
    I still have Xcode warnings after removing the margin constraints and couldn't figure out why. Restarted Xcode and they're gone. Sigh. – progrmr Sep 23 '14 at 14:22
  • 1
    Turned it off, and then somehow it came back mysteriously. Is there a way to disable it globally? – David Liu Oct 02 '14 at 13:39
  • this is a major headache. has anyone figured out how to resolve this? every time i add leading/trailing/top/bottom constraints to the margins it adds "relative to margin" and it makes the app look horrible on iOS 7. – Joris Weimar Oct 16 '14 at 14:41
  • @Legoless if you aren't using storyboards what would be the correct code to use? – Cody Weaver Aug 31 '15 at 22:12
  • Is there a way to let it default to NOT creating a constraint relative to margin? Most of the apps that I create have background images that goes all the way to the side, and having to untick this option in in each of the constraint is really annoying. – Justin Leo Oct 04 '15 at 08:27
61

Disable "Prefer margin relative"

enter image description here

Andy
  • 14,260
  • 4
  • 43
  • 56
  • 1
    Each time I look at my constraints on this Pin view, the Prefer margin relative checkbox is ticked (iOS beta 5, Xcode beta 6). I found it wasn't enough to just untick this, but had to remove any constraints created under prefer margin relative, and RECREATE THEM using this popup with the checkbox unticked. It still defaults to showing them as Prefer margin relative, but as long as they are created with it switched off, my layout seems to work fine (rather than being totally screwed). I normally create constraints by ctrl-dragging, but found this just wasn't working for me at all. – Rob Glassey Aug 19 '14 at 20:39
  • 1
    I've since found [this](http://stackoverflow.com/a/25332026/1320544) which gives an easier way to edit these without deleting and recreating them – Rob Glassey Aug 19 '14 at 21:02
  • 4
    If you create constraints by ctrl-dragging, note that if you hold down Option while the floating menu that pops up is displayed, it'll toggle whether constrain to the margin or not (you'll see the description change in the menu.) Can be helpful if you don't want to change the overall setting or edit them later. – Matt Gibson Sep 08 '14 at 16:11
  • 1
    Can someone explain the difference between **relative to container** and **relative to container margin**? – Sergey M Sep 20 '14 at 05:40
  • If you need a quick way to find all of the constraints that have relative margin set, open all of them in the left panel, and have the inspector panel open. Those constraints that are relative to margin will have it in the constraint description - ie 'titleLabel.leading Margin' as opposed to 'titleLabel.leading' – Ying Sep 26 '14 at 15:31
38

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.

Matt R
  • 804
  • 1
  • 9
  • 8
  • This was far more helpful for me. – Oxcug Aug 15 '14 at 15:38
  • I am getting a few instances of the warning, but can't find the string "Margin" in my storyboard source file. Grr. Scratch that!I was in the wrong project window. – VaporwareWolf Sep 11 '14 at 22:39
  • Doh! I figured this out for myself before reading your answer! Would have saved some time. Anyway, yes, this is a good way of locating the offending constraints. – siburb Sep 25 '14 at 03:16
6

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.

Jonathan Arbogast
  • 9,620
  • 4
  • 35
  • 47
  • 1
    Thanks for the Option key trick ! Didn't know that and I was always wondering how to add other constraints than those suggested. – Petar Sep 12 '14 at 15:17
  • hold option and click where? it is not working for me. – Duck Oct 08 '14 at 05:35
4

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:

  1. Close Xcode
  2. Open your stroyboard file in your favorite text editor, that supports regexp.
  3. Find and remove from storyboard file all strings by pattern: .*"baseline".*\n
  4. Now save file and open it in Xcode
  5. Fix all appeared misaligns: find all warnings and press "Update constraints for all views" to save original position of all views.
  6. 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

Community
  • 1
  • 1
skywinder
  • 21,291
  • 15
  • 93
  • 123
3

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.

railwayparade
  • 5,154
  • 1
  • 39
  • 49
ShakeMan
  • 481
  • 1
  • 5
  • 10
  • 2
    Much easier method is to right-click on the storyboard within Xcode and select Open As > Source Code. Then just do a search for 'margin' and remove any constraints that reference a margin. Once removed, Open As > IB Storyboard and fix any missing constraints. – Adolfo Sep 12 '14 at 18:50
  • 1
    Just be aware that the error can continue to show in Xcode after you have removed the offending constraints. So if you can't find the word 'margin' you should exit and restart Xcode. I won't say how many times this has tripped me up! – Fran K. Oct 16 '14 at 14:43
0

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.

ObjectiveTC
  • 2,477
  • 30
  • 22
  • Why are you talking about "first baseline" when the question here is about "attributes relative to the layout margin"? It's a different error, though the cause and fix are similar. – Bryan Nov 01 '14 at 18:59
  • To clarify, the error I received was: "Attribute Unavailable: First baseline layout attribute on iOS versions prior to 8.0". Perhaps this issue deserves its own topic.. – ObjectiveTC Nov 04 '14 at 01:54
  • I was getting this same "First baseline" issue. It's a similar fix, but I added a new question for it since google only brought me here. http://stackoverflow.com/questions/27557692/attribute-unavailable-first-baseline-layout-attribute-on-ios-versions-prior-to/ – djburdick Dec 18 '14 at 23:45