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

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.

Related

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".

Launch image adds margin

After adding my launch image using a xib (and I even tried the traditional launch image way as well) some of my view controllers have a strange right margin now. For example before I might have had a label whose width was the entire screen, and used auto layouts such that it was leading/trailing to the content margins, and now despite that after adding a launch image there is a margin (it's not on every screen, but definitely some). On one of my screens where this is most prevalent i'm using autolayout but overriding with constraints that I add programmatically. Not sure if that's related.
Has anyone experienced anything like this or does anyone know if adding launch screens affect the story board / view controllers in any way?
Thanks!
On the launch screen select your image view, then select the pin at the bottom right of xcode (3rd option). Make sure all constraints are set to 0, while making sure the red I is selected for each one when they are set and uncheck constraint to margins. Finally click add constraints :)
Hope this helps you!
Rachel

Launch Screen XIB: Missing Width / Height Constraints (Xcode 6)

Using Xcode 6, I am building an iOS app that targets iOS8.
I'm working on a LaunchScreen.xib file, created from the menu: File > New > File > iOS - User Interface - Launch Screen. "Use Auto Layout" is enabled.
Inside the XIB's main UIView, I've placed a UIImageView. I would like to add width and height constraints, so that the UIImageView has the same size as the container. However, Xcode doesn't let me do that: I can only work with spacing and alignment.
Here's what I see:
What am I doing wrong? Why can't I see a menu like the following?
I know I could reach the same result by setting leading space, trailing space, top space, and bottom space to container to 0, but I would also like to understand what's happening here.
EDIT:
Xcode 7+ defaults to a Storyboard file for the Launch Screen.
As explained below, unlike XIBs, Storyboard files allow you to set width and height constraints to the root UIView.
I haven't been able to find an official explanation as to why XIBs behave like that, when they have a UIView at the root.
What looks weird to me is that what we are provided with as a default (LaunchScreen.xib) isn't set up to behave exactly like the Storyboards we've got used to.
The first solution I opted for was using a Storyboard to draw the Launch Screen, instead of a XIB:
Delete LaunchScreen.xib
Create a LaunchScreen.storyboard (Menu: File > New > File > iOS - User Interface - Storyboard)
Go to your project properties, select navigate to your target's general properties and select LaunchScreen.storyboard in the "Launch Screen File" combo box
Open LaunchScreen.storyboard, add a View Controller, and make sure to tick the "Is Initial View Controller Box"
You can now work on the View Controller's View as your Launch Screen, and you'll get the width and height constraints I was initially looking for
An alternative solution is removing the default UIView in LaunchScreen.xib and replacing it with a View Controller.
Just like in the previous case, if you do so, you can work on the View Controller's View, and you'll get the usual constraint menu.
That happens for every .xib, you just can't make a view of equal width than the main view. On the other hand it is possible to do it when you are in a story board.
Finally as you said you can accomplish the desired layout by setting the leading, trailing top and bottom distances.
Will your app run on a single device? If not how do you know the size of the container? It will vary between an iPhone 5, iPhone 6, iPod or iPad. The best way is still to specify leading space, trailing space, top space and bottom space to 0. You're sure it will work on any device.
Anyway, I tried to reproduce the problem, but I am able to specify the width and height of the image view when going in the Pin menu whether in Editor/Pin or by pressing the button at the bottom of the editor. I can specify any width/height with no problem.
Hope that helps!
A very simple solution is to add a clear UIView and pin it to the container, then you can access all the constraints by associating them with the clear "root" UIView you have added.

iOS Simulator/Interface Builder off-center

In interface builder I placed a button here:
However when I run the iOS Simulator (device: iPhone 5s) it appears here:
I am using Xcode 6 Beta 4
I would recommend you to use size classes due to new iOS screen sizes, but you still can disable size classes on the interface builder in the file inspector as you can see on the image:
If you would like to use Autolayout you can do it adding the next constraints (see the first image) in the corresponding View. In the second step you should use the width and height corresponding to your View. In the second image you can see a recapitulation of all the constraints and a simulator screenshot.
Images:
This is not actually anything to do with size classes. This is due to the fact that it is assuming you have placed it approx. 200 points from the left edge.
What you haven't done is added AutoLayout constraints to say that you actually mean the centre.
CTRL-drag from the button to the view and add...
Center X values
Top space to superview
(Or something like those)
Tha will make it work.
Old question, but still question... You have 2 chooses:
Disable auto-layout
Use auto-layout to center this button on any size of display (go to "Add New Alignment Constrains", check "Horizontally in Container" and then click "Add Constrains")
(Xcode 10.1)
In the small Triangle Menu: Select "Clear Constraints"
Then Select "Add Missing Constraints"
You can also manually adjust constraints by selecting one in the scene.
Then grab the constraint handle.

Resources