I'm fairly new to Interface Builder and I'm trying to practice it with a very simple view. Basically, I have 3 UIViews on my view controller. The problem I am having is that whenever I adjust the frame of 1 of the UIViews, the other 2 will disappear.
Here is a short 7 second video of the problem: https://vine.co/v/OMlbMIdWMe5
If I run the app on my device, I only see 1 of the views (the 1 I was adjusting) - the other 2 remain hidden.
I am using Xcode 6.1 . What could be causing this?
***EDIT: If I add 1 constraint to a view, it won't disappear when I readjust the frame of the other view. That is:
Add a constraint to ViewOne then readjusting size of ViewTwo will only cause ViewThree (without any constraints) to disappear.
So the solution is to add constraints to all 3 views before readjusting the frame of any of them. But still, what could be causing this issue?
This is an issue with Xcode 6.1, and has been reported to apple. A similar post can be found here.
WORKAROUND: I have found that you can edit the constraints in the right hand pane of the storyboard editor, in the size inspector. It actually does not prove that tedious.
You can disable autolayout if you don't need it, that helps.
Otherwise, wait for next patch~
Related
I have an issue that makes no sense to me. I have the following setup:
| UITableView |
|| UITableView header ||
||| UIView |||
So inside my UITableView header I have a container view that has leading, trailing and top constraints to its superview. Everything is set up correctly for my test device size (which is iPhone 8). If I change the test device to iPhone 8 Plus I get a strange offset for my trailing constraint - 39pt to the right edge, which is exactly the difference between iPhone 8 plus width in points and iPhone 8.
When I switch between devices in Xcode and see that the trailing constraint is not correct I just make an adjustment myself (change trailing to 1 and then back to 0) and the problem goes away for the particular device.
Initially I thought it is bug in Xcode but when I tested on a device the problem is still there.
I tried setting up a new view controller and adding the same elements but with no effect.
I am attaching screenshots to make my issue clearer.
Before: Adjusting the desired constraints
After: Switching to a device with different size
Adding this as an answer because it's too much for a comment - even though it's really just a confirmation, not a solution.
OK - looked at your project.
I'd say it's an IB / Storyboard bug, which I've seen in other circumstances. If you change the View As... device, the frame does not update immediately.
However, if you change anything that would cause a layout update - such as temporarily changing the background color of a view or font size of a label - everything should snap into place.
You'll also notice that if you select an element and move it slightly, the Update Frames button / menu item becomes enabled... and that will also correctly update the frames.
Note: When I ran the app, regardless of how the layout looked in Storyboard, the constraints correctly sized the views at run-time.
At first I accepted DonMag's answer (thanks for your time) as this really seemed to be an Xcode Interface Builder bug. As I investigated further when having the scenario I mentioned auto layout constraints are not updating the layout when I need it hence not giving me the right view.bounds.
I tried getting it in viewDidLayoutSubviews() without success as well - it was still giving me a size that suits another device.
What did the trick was calling view.layoutIfNeeded() before working with view's bounds. What it does is to update the view's layout immediately. As a result you can access the desired view's bounds.
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.
I built a custom view controller. It has 1 tableview, 2 views and 1 map. When I set the constraints for this layout it works fine. I close project and reopen the map view out of the view controller. The width and height are 1000 points. If I set the map on top of this stack, there is no problem. But when I set map in the middle of the other views, a problem happens. I must put this mapview inside another view or it affects the others, but the problem is still there.
I use Xcode 8 beta 5, and constraint is 0 0 0 0
I have the same issue in the latest beta. I believe the problem is in the wrong default size, that is always set to 1000x1000 by Xcode for MKMapView.
I would wait for the fix in the next Xcode version.
If you, by some reason, really need it fixed now, you can setup a UIView with constraints as required, then put MKMapView inside the UIView and use Autoresizing to scale it.
I'm working on my first swift app and I found out quickly that I need to use constraints to properly layout my page. So far I'm running into two issue.
The first one is that after adding some constraints, my view is pushed off the screen on the storyboard, but it looks fine(outside of my second issue) in the Assistant editor.
My second issue is that on some phone sizes the constraints are causing the text fields to expand when viewed in the Assistant editor and on an iPhone 6s test device.
I've added some screen shots below. Any help with this issue would be really appreciated.
Each time you add a constraint in storyboard, it is not automatically applied to your working view. You may think it should be applied immediately, but try to think think like this. If you have a view, and you apply height constraint (for example) and if Xcode applied it automatically you would get a view of width 0, since you haven't applied width constraint. That's why you need to tell Xcode to update constraints explicitly.
So when you want to update constraints, select view you want to update and go to Resolve Auto Layout issues, and click on Update Frames. This is located in the bottom right corner of your storyboard. It looks like a small triangle between two lines. Here you can update all views or just selected views.
For the second issue, you need to apply fixed width constraint to your stack view, instead of adding leading and trailing constraints. By doing that stack view will have the same width on each device.
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.