Misplaced views each time Xcode restarts

IosObjective CXcodeInterface BuilderAutolayout

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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.

Solution 3 - Ios

In my case it happens for all labels / buttons with custom fonts and intrinsic (not explicitly defined) sizes. Looks like an Xcode bug.

Solution 4 - Ios

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 :)

Solution 5 - Ios

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

Solution 6 - Ios

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.

Solution 7 - Ios

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

Solution 8 - Ios

  1. click to preview a regular file (not .xib or .storyboard file)
  2. quit Xcode
  3. run git checkout .
  4. restart Xcode

the .storyboard file is not modified now.

Solution 9 - Ios

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

Solution 10 - Ios

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.

Solution 11 - Ios

First check you have implement all constraints.If all constraint are correct then delete related constraint and rebut again.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondjibouti33View Question on Stackoverflow
Solution 1 - IosSushree SwagatikaView Answer on Stackoverflow
Solution 2 - IosSHNView Answer on Stackoverflow
Solution 3 - IosSoftDesignerView Answer on Stackoverflow
Solution 4 - IosXinyuView Answer on Stackoverflow
Solution 5 - IosSameh SalamaView Answer on Stackoverflow
Solution 6 - IosNupur GuptaView Answer on Stackoverflow
Solution 7 - Iosuser4886069View Answer on Stackoverflow
Solution 8 - IosLongfei WuView Answer on Stackoverflow
Solution 9 - IosAllenView Answer on Stackoverflow
Solution 10 - Iospriya bagrechaView Answer on Stackoverflow
Solution 11 - Iosuser2732084View Answer on Stackoverflow