IB Constraints dont show the same on device - xcode

I have a problem with Xcode IB constraints. To be frank, I never used it and now I inherited a project where I need to bring a fix.
IB shows things correctly, but at run time I get somethin different. I tried to play with some of the parameters in IB, but nothing helps.
So, my question is: How can I get on the device what I see in IB.
Here's IB:
On the device, the 'Fin de Course' is going into next field and Promo Code gets stuck.
What can I do?

I assumed that you already set the constraint correctly but your view is different when the app is running compared to Interface Builder or Storyboard. My suggestion is try to check the "orange" constraint. This is where the view gets "wrong" position on runtime.
The solution is to click the view which has "orange" constraint, then press Cmd + Opt + = in the keyboard. You will see the position will be updated.
But if you want to change the position of the view, you cannot just drag and drop if the constraints are still exist. The old constraint will still active. You have to update the constraint manually. You also can delete all constraints of the view first, then drag and drop the view into your desired location, and then set the new constraint.

Related

Xcode Interface Builder 9.1 Collection View Cells have too small "expected height"

Recently, I encountered a problem with the Interface Builder which I failed to solve. I'm using a UICollectionViewController and whenever I add a Cell it immediately shows a orange rectangle (indicating a auto-layout update). Updating the frame/cell doesn't change anything. The warning associated with the orange marking reads "Expected: height=Y, Actual: height=X" where Y is always exactly X-100. That is: When I set a custom cell height of 193, it says "Expected: height=93". This happens no matter which size I enter.
The problem with this is that I have many difficulties with the subviews and their constraints. For instance, if I add a subview with the constraint to keep the same height as the cell, it will be shown as 93 (the expected height), even though my cell should be 193. Layouting becomes impossible.
I tried adding a new cell, same issue. I tried removing every subview and re-adding them, nothing.
Any ideas are highly welcome.
Thank you very much!
Solution found ! (Xcode 9.2)
The bug seems to only happen when you try to embed the UICollectionViewController in a Container View in the storyboard. The workaround is to remove the segue while you set your constraints, and re-add the segue once your constraints are well set.
I also think that setting UICollectionViewController size as Freeform could be responsible of the bug. If so, try to set constraints before changing the size.
Hope this helps !
I'm not sure if this solved it, but the warning and issue disappeared once I did the following:
Select the CollectionViewController in Interface Builder and set the size to freeform and make the height larger, large enough to show all your template cells. (Plus: maybe refresh the views). That solved the issue for me.
In my case, I have added two collection view cell, withing collection view Controller.Only setting the view controller to free form wont help us. Need to update the size, to get rid of the warning.
updating collection view cell frames, wont help us. Warning will be continuously changing its length.
I tried to work without considering the warning, as I have set my collection view cell size programmatically and it does working as expected.
I think it's just a strange bug
I delete the collectionViewController in SB, then create a new one, the problem disappear
This is a common bug with Interface Builder. I don't know the exact cause, but I believe it's something to do with placing collection views inside arrangement views that do not have an exact size. I've seen it when putting them in scroll views, stack views and container views.
There is a simple fix, although I believe this to be a bug in IB that ought to be corrected by Apple rather than hacking around it. The solution (for me at least - your mileage may vary!) is to place the collection view inside an ordinary UIView and pin its edges to it, then place that parent view in the position that you wanted your collection view with all the constraints it required.
I usually found that there were actually no auto layout issues at run-time, which is why I believe this to be purely an Interface Builder bug.

Using "View As" device other than active device results in layout issues

This one is best demonstrated with a video. The layout of my view changes based on which device I "View As: " in Interface Builder. It looks like changing this setting modifies the rects in Interface Builder, and then those changes stick. Is there a better way to prevent this from happening?
(It doesn't matter which device I use. If "View As" doesn't match the device, something will appear off.)
I figured this one out. It was a missing constraint. I was adding my view to a page view controller, but forgot to constraint the child view in the process:
marqueeViewController.addChildViewController(pageViewController)
marqueeViewController.containerView.addSubview(pageViewController.view)
// ** This constraint was missing, and is needed to resize the child view. **
Layout.edges(parent: marqueeViewController.containerView, child: pageViewController.view)
The rectangle of my views were changing size as I changed the "View As:" setting in Interface Builder. And since there was no constraint to reel them back in, they stayed set at the different size.

Xcode 8 attribute "hidden" does not hide view in Interface Builder

seems like Apple change how Interface Builder behaves in Xcode 8? Because when I check hidden in Attributes Inspector on a view in Interface Builder, that view is still visible.
This makes it very tedious to work with views where some views needs to be the view with the highest "z value", the front most view that is.
Is there some other way to show the green view in this example, than to change the order of them to the right (i.e. change their "z value")
In the image below hidden is checked, but I still don't see the green view below. You can download this trivial project at github
When the project is run, the green view is indeed shown, but the issues is that it is annoying when working in Interface Builder.
Am I missing something?
I have the same opinion on it and I also believe that it's annoying. I with they gave you the choice to update the actual storyboard before runtime or not but they didn't so for now we have to deal with it.
There is a quick alternative option though. Hidden will not update in the storyboard but alpha will. If you change the alpha it will update in the storyboard so if you want to see the view behind it just change alpha to 0. You can always change it back easily or if your doing it in code, instead of unhiding your view just change the code so the alpha is set to 1.
How about unchecking the installed checkbox of the red view?
This has also the flaw that you have to remember to reinstall it, but you don't have to change the z-order of your views.
This is deliberate. We wouldn't want a view to be hidden from you, the editor, just because it will be hidden when the app runs. You can easily select a covered view, such as the green view, using Shift-Control-Click on the red view (or use the document outline at the left of your screen shot).

Xcode auto layout buttons not resizing

I have looked at many tutorials and just can't seem to get Buttons to resize and layout properly using Xcode 6 auto layout.
The tutorials that make the most sense just use Views as examples.
Trying to build a Universal soundboard app with buttons arranged in the attached picture.
I also tried putting all the buttons in one View container but still no luck.
What am i missing?
Thanks so much
Select the button you want the constraint on, and command click it's parent view.
Next, select add new constraint (at the bottom of xcode), click "Equal Widths", then click add constraint.
Select the object.
Double-click on the constraint rectangle (not obvious! "Edit" takes you somewhere else)
Then at the right hand side of xcode the attribute inspector should
come up:
At the multiplier property you can make a ratio or a decimal of the percentage value you want. If you want the button to be one fourth of it's parent view, then set the multiplier to 1:4.
This might not be the most ideal answer, but it's the best I can offer. Hope this helps!! Good luck!
You can check out this
How to create equal spacing between multiple label in ios xcode 6.3 using AutoLayout Constraints
You can also check out Evenly Spacing Views in Auto Layout
If you still have problem then comment me I will try to fix your issue

A popup, "Failed to automatically update constraints" pops up in Xcode 6.3.1

When I added a couple of controls in my viewcontroller in storyboard and click
Resolve Auto Layout Issues -> Reset to Suggested Constraints.
The following popup appears and I cannot perform auto layout constraints.
Though, when I remove the Prototype Cell (which has a couple of labels and an imageview) from my tableview then auto layout works fine. Any thoughts?
Edited:
And manually adding constraints to individual elements work fine as well. After manually setting the image view inside prototype cell, the auto layout has started working fine without popping up that popup. So, maybe, sometime, it is hard for auto layout to figure out all these constraints itself.
Try clearing constraints before resetting them, this has worked for me before.
I had the same problem when using prototype cells in a UITableView and some other views in a UIViewController.
My solution was to select all the elements in the prototype cell and see if these had red or orange markers (constraint conflicts/problems). These elements I cleared the constraints for and afterwards I had no problem telling the entire view to "Reset to Suggested Constraints".
Obviously, Interface Builder has a problem resetting embedded table content constraints.
Actually . If you import different storyboard from another Xcode project. This is not possible to avoid problem. My personal advice is create a new UIViewcontroller or stroyboard. And step by step add items. But. I did not do this.
I lived three times. In fact When I saw this message. I was configuring tableview or UITableviewcell. At the end of the story I have fixed my problem. But There is no gold key for issue. I will explain as much as possible.
for All Views in View Controller select Clear Constraints
If you are using tableview or collection view. please delete.
I assume. there are label, button and scrollview on the project. Please add missing constraints. If you leave problem again. You need to delete any of them.
for All Views in View Controller Reset to Suggested constraints.
If you don't leave problem again. PERFECT. If there is a reverse situation
for All Views in Second View Controller select Clear Constraints
delete any of item. Select add missing constraints again.
Continue to try the third and fourth steps. Until you find the faulty objects.
I'll try to help you if you submit your project.
Good luck .
Following Solution work for me.
Actually initially i used Free-from storyboard of size 600 into 900 that time i was getting this error. so i deleted that storyboard and again drag and drop element and make connections, then i solved issue.
I have faced same problem with Xcode 6. In my MainViewController I have a tableview with custom TableviewCell. In Interface builder itself I have taken taken 2 labels on tableview cell.
When I try to add constraints it gives me error. I did remove prototype cell in tableview and worked with nib with customCell. Be careful while working with custom tableViewCell.
Let me know if you need any additional information.
I had same issue last evening, After many try and error i found that it was just a corrupt ViewController. I created a new ViewController2 with the same aspects, then applied constraints to the ViewController2, and it worked! Still not sure why exactly the other one had issues.
I had some problem with Xcode 7. I solved it very simple. I cleared constraints -
Reset to Suggested Constrains
, then applied -
Add Missing Constrains.
This has worked for me.

Resources