Attribute Unavailable warning on iOS versions prior to 8.0 - ios

What's causing this warning in the interface builder?
Attribute Unavailable
Layout attributes relative to the layout margin on iOS versions prior to 8.0.
I'm using the XCode 6 Beta. The warnings are pointing to lines in the xml source that do not concern attributes.

Since I had already created the constraints, the answer was to click on the constraint, and then look in the Utilities bar (on the right). As shown in the picture below, click on an item and then make sure that "relative to margin" is unchecked.

When you add constraints in Xcode 6, you have the option "Constrain to margins" which create a different constraint and can't be read, for example, in Xcode 5.
Create the constraints without this options and the warnings should go away. This constraints will be available on iOS 8.
If you have already created the constraints, we can change it clicking on the constraint and editing it. When selecting the second item, you can uncheck the "Relative to margin" option (as explained in the GLee answer.

If you can't find where the warning is coming from you can use this grep to find it.
Run this in Terminal in your project directory:
grep -C 6 -nr Margin .
The 6 says how many lines of context to show before and after the match.

As per Gasper Kolenc opening the storyboard as Source Code is very useful. Here is a bit more info from my experience fixing this problem:
In the storyboard source code search for "margin".You will see a line such as this.
constraint firstAttribute="trailingMargin" secondItem="Pgb-8w-IHT" secondAttribute="trailing" constant="8" id="nKP-uZ-7Xm"/
Find the id of one of the attributes (Pgb-8w-IHT in the example above).
Scroll up to find in which object this constraint belongs. Something like:
tableViewCell contentMode="scaleToFill" selectionStyle="default"
Switch back to storyboard view and show the identity inspector window.
Search through the subviews of the object you found above to find the id (Pgb-8w-IHT) on the identity inspector.
Once found inspect the constraints to find the one that needs to be deleted and recreated without the constrain to margins option selected (use the constant=" " as a hint".

In my case, I had to uncheck the "Use Auto Layout" from my storyboard in the File Inspector, and redo all the constraints in my app, the old way with that shrinking / enlarging red rectangle.
Size Classes were already disabled and the target is IOS 7.1.
But think well if you have too many View Controllers, and had already made all your constraints. When you disable auto layout, Xcode clears all the automatic constraints and reverts to the old model. Then you have to do all the job again.
This was the only way I get rid of that Attribute Unavailable warning.

Related

Where is the "Use Auto Layout" checkbox in Xcode 12?

I have started using Xcode for the first time in a long time.
The version of Xcode is 12.
I created a new project of Swift with the interface of Storyboard instead of SwiftUI.
Then I selected Main.storyboard in the left pane of Xcode and put a UILabel onto the View Controller.
I found that there was not the "Use Auto Layout" checkbox which had been in the right pane of the Inteface Builder of old Xcode.
Where is it in the version 12 of Xcode?
Is there a way to use the auto layout system in Xcode 12?
The checkbox was removed because autolayout is now the default. Just make a constraint and it springs to life for any view(s) affected by that constraint.
A view completely untouched by constraints still uses the auto resizing mask.
There is a way to subvert that behavior for an individual view; but don't.

The size of objects in xcode get changed automatically [duplicate]

I'm using storyboards for the first time in my app
When I close Xcode, I have 0 warnings about misplaced views or anything pertaining to my storyboard. When I restart Xcode and open the storyboard, 3 out of my 15 scenes have misplaced view warnings.
Without touching anything (other than selecting the file), I can issue a git status and see that the .storyboard file has changes. If I git diff, I see this included in multiple areas of the file:
<variation key="widthClass=compact" misplaced="YES">
<rect key="frame" x="8" y="56" width="130" height="34"/>
</variation>
If an element already had <variation key="widthClass=Compact" node, then the misplaced attribute and rect node are added.
The only thing I have to do to get the warnings to go away is click on each one, make sure "Update Frame" is selected, and click "Fix Misplacement". That fixes everything until I restart Xcode.
The basic structure of my scene is:
UIView
UICollectionView
UICollectionReusableView
UICollectionViewCell
UICollectionViewCell
UISegmentedControl
How can I prevent this from happening each time I restart?
This generally happens when you have not set the constraints properly.
Try removing all views inside the UIViewController and adding newly again along with your constraints.
Clean and run your code.
One possible reason for misplaced views is:
If you have used live views(IBDesignables) in storyboards then if you open the storyboard and quickly move to some other file before the IB has finished building all the live views then the the views get misplaced. The IB starts building the live views as soon as you view the story board in the editor. To fix the misplaced views, allow IB some time to finish building the storyboard by staying on the storyboard for few moments, and the misplaced views will get fixed.
In my case it happens for all labels / buttons with custom fonts and intrinsic (not explicitly defined) sizes. Looks like an Xcode bug.
I face the same issue before, and I suppose it's not our fault, it's just a Xcode's bug.
If your .storyboard file changed but you didn't touch anything, just select the file and select the Xcode menu Source Control -> Discard Changes in "xxxx.storyboard"..., it should be work :)
This issue is caused when you add constraints that don't fit the actual screen size at run time.
for example : if you have a UIButton with width that exceeds screen width (let's say width 600) and you add fixed width constraint to that UIButton but no leading or trailing constraints , Xcode won't give you warnings as there are no conflicts, but when you actually run the app on a device or simulator with screen width less than 600, Xcode will automatically adjust UIButton width to fit the screen width which is less than 600, causing your layout constraints to change to what fitted the screen at runtime
If you are using size classes then either your constraints are not up to the mark (according to all device type if Any-Any). If not then try to adjust the frame or constraints and check once again.
Auto layout may also cause this issue.
You will get all the information regarding the warning once you click on them.
I used to face the same problem with my previous app. What I noticed is that the size of storyboards matters here.
This is Xcode issue.
If we use storyboard of any width any height, this problem doesn't occur.
If you are using storyboards of size Compact width regular height, this issue occurs.
So try to use default storyboard sizes in Xcode
In the bottom of the storyboard we have the option to select the size class. If I change the option from wRegular hRegular to any other option I get the similar issue. I don't know the reason for this.
Try to resolve it by selecting the same size class after opening the project as you have selected before closing the project.
For example, if you have selected the size class as wCompact hRegular for iPhone and you have closed your project. After opening the project again, you will see that the size class has changed to wRegular hRegular. Because of this the view objects change. Select wCompact hRegular again to resolve the issue.
Hope it will help.
A simple fix may be to simply allow Xcode to choose constraints for you, then go back and check what it has chosen to be sure it will work for you.
In storyboard:
Select View Controller giving you problems
Resolve Auto Layout Issues (bottom right)
Selected Views: Reset to Suggested
Constraints
To check what constraints have been reset:
Select each View within controller
Constraints Inspector (top right)
scroll down and examine constraints
click to preview a regular file (not .xib or .storyboard file)
quit Xcode
run git checkout .
restart Xcode
the .storyboard file is not modified now.
First check you have implement all constraints.If all constraint are correct then delete related constraint and rebut again.

XCode: How to find all then delete all disable constraints

I am new in iOS. I see a lot of disable constraints in my Main.storyboard and I want to delete it.
However, my Main.storyboard have many screen so I want to find the way to find all disable constraints then delete all.
Is it possible with Xcode?
Or any way to prevent disable constraint generate.
Any help would be great appreciate
Obviously there's the manual way, of going through all of the constraints in Interface Builder and just deleting the disabled ones.
But, if you have a tonne of disabled ones and you just want to wipe them all out, you can edit the storyboard file directly (do this with Xcode closed).
You could build a script to this, but here's the manual process to start with.
Look in the Storyboard file for lines such as: <exclude reference="egm-9S-fZb"/>
In this case, egm-9S-fZb is the id of the constraint that's disabled.
Now find a line in the storyboard like this: <constraint ... id="egm-9S-fZb"/>
Delete both of those lines and you've deleted that disabled constraint.
Note: Be aware that if you have constraints that are disabled in certain conditions, i.e., you have a constraint that's only enabled for say Compact Width, Compact Height, it will be listed with an exclude line, as well as an include line. So check for an include line before deleting the exclude and the constraint lines.
Disable constraints are generated when you create a constraint and delete it from under "Size inspector (one with ruler like)". Just remember to delete it from your scene, then you will not have disable constraint floating around...or just delete it when u see it :).
#Dace Wood and #Bao Tran already said some useful measures. I would like to add something.
If these disabled constraints are only for a specific view object, you can manually delete those disabled constraints from IB and then add new ones. Or to take risk, you can use the "Reset to suggested Constraints" or "Add Missing Constraints" seen in "Resolve Auto Layout issues" seen in bottom right part of storyboard. It is really helpful.

Misplaced views each time Xcode restarts

I'm using storyboards for the first time in my app
When I close Xcode, I have 0 warnings about misplaced views or anything pertaining to my storyboard. When I restart Xcode and open the storyboard, 3 out of my 15 scenes have misplaced view warnings.
Without touching anything (other than selecting the file), I can issue a git status and see that the .storyboard file has changes. If I git diff, I see this included in multiple areas of the file:
<variation key="widthClass=compact" misplaced="YES">
<rect key="frame" x="8" y="56" width="130" height="34"/>
</variation>
If an element already had <variation key="widthClass=Compact" node, then the misplaced attribute and rect node are added.
The only thing I have to do to get the warnings to go away is click on each one, make sure "Update Frame" is selected, and click "Fix Misplacement". That fixes everything until I restart Xcode.
The basic structure of my scene is:
UIView
UICollectionView
UICollectionReusableView
UICollectionViewCell
UICollectionViewCell
UISegmentedControl
How can I prevent this from happening each time I restart?
This generally happens when you have not set the constraints properly.
Try removing all views inside the UIViewController and adding newly again along with your constraints.
Clean and run your code.
One possible reason for misplaced views is:
If you have used live views(IBDesignables) in storyboards then if you open the storyboard and quickly move to some other file before the IB has finished building all the live views then the the views get misplaced. The IB starts building the live views as soon as you view the story board in the editor. To fix the misplaced views, allow IB some time to finish building the storyboard by staying on the storyboard for few moments, and the misplaced views will get fixed.
In my case it happens for all labels / buttons with custom fonts and intrinsic (not explicitly defined) sizes. Looks like an Xcode bug.
I face the same issue before, and I suppose it's not our fault, it's just a Xcode's bug.
If your .storyboard file changed but you didn't touch anything, just select the file and select the Xcode menu Source Control -> Discard Changes in "xxxx.storyboard"..., it should be work :)
This issue is caused when you add constraints that don't fit the actual screen size at run time.
for example : if you have a UIButton with width that exceeds screen width (let's say width 600) and you add fixed width constraint to that UIButton but no leading or trailing constraints , Xcode won't give you warnings as there are no conflicts, but when you actually run the app on a device or simulator with screen width less than 600, Xcode will automatically adjust UIButton width to fit the screen width which is less than 600, causing your layout constraints to change to what fitted the screen at runtime
If you are using size classes then either your constraints are not up to the mark (according to all device type if Any-Any). If not then try to adjust the frame or constraints and check once again.
Auto layout may also cause this issue.
You will get all the information regarding the warning once you click on them.
I used to face the same problem with my previous app. What I noticed is that the size of storyboards matters here.
This is Xcode issue.
If we use storyboard of any width any height, this problem doesn't occur.
If you are using storyboards of size Compact width regular height, this issue occurs.
So try to use default storyboard sizes in Xcode
In the bottom of the storyboard we have the option to select the size class. If I change the option from wRegular hRegular to any other option I get the similar issue. I don't know the reason for this.
Try to resolve it by selecting the same size class after opening the project as you have selected before closing the project.
For example, if you have selected the size class as wCompact hRegular for iPhone and you have closed your project. After opening the project again, you will see that the size class has changed to wRegular hRegular. Because of this the view objects change. Select wCompact hRegular again to resolve the issue.
Hope it will help.
A simple fix may be to simply allow Xcode to choose constraints for you, then go back and check what it has chosen to be sure it will work for you.
In storyboard:
Select View Controller giving you problems
Resolve Auto Layout Issues (bottom right)
Selected Views: Reset to Suggested
Constraints
To check what constraints have been reset:
Select each View within controller
Constraints Inspector (top right)
scroll down and examine constraints
click to preview a regular file (not .xib or .storyboard file)
quit Xcode
run git checkout .
restart Xcode
the .storyboard file is not modified now.
First check you have implement all constraints.If all constraint are correct then delete related constraint and rebut again.

View doesn't fit in iOS Simulator

This must be a dumb question, but nevertheless I ask it. I don't have to explain (I think) that I'm just beginning with Xcode.
I have created this view in IB:
And this is the result in the simulator:
What can I do to fix this. It doesn't matter what device I choose (eg. 4S or 6) and device options are in sync (= same in IB and simulator).
NB: It is a new project and I have reset the auto-layout constraint to suggested settings.
You need to ensure your Auto Layout constrains are accurate.
Make sure the view a controller is selected, and choose "Clear constraints"
Select each of these items and choose "center horizontally in container"
Select each of these, and add a constraint linking each to the one above. Note Xcode will fill in their current positions as default values.
Auto Layout tip: name your items in the document outline (the left in IB). Then when you examine constraints, you will be able to see which ones they are attached to by looking in the size inspector in the right.
Watch this video and get familiar with Auto-Layout.
https://www.youtube.com/watch?v=G53PuA_TlXk
Letting Xcode use "suggested constraints" is nearly always useless and won't work like you expect. So you'll either need to position all of your elements programmatically by changing their frame, or, you need to set up proper auto-layout constraints.
Your problem is that you are not using Autolayout functionality. I Would recommend reading about it, especially if you are new to iOS Dev. You really can do a lot if you are using IB (Storyboards) - check out this tutorial is should get you started
Autolayout is a system that helps you create apps for all different device dimensions.
You need to go into attributes inspector for the view controller (right sided panel) go Simulated Metrics(should be at the top) -> Size and change it to "iPhone 4S" or "iPhone 5".

Resources