I'm having an issue using Xcode 8.1 that I haven't had before. I have a UITableView inside a ViewController and have pinned it using constraints to top (0), left (0), right (0) and bottom (0).
When I've done this before the table view expands and contracts so is always centred on all screen sizes.........but please see the screenshot for my current results.
I've tried removing all constraints associated with that view and trying "Reset to Suggested Constraints" but that didnt change anything and have also attempted to build all the constraints back in manually, but still no luck.
Any suggestions or am I missing something really obvious!
Thanks
"Reset to Suggested Constraints" nearly always gives you something you don't want. It looks like your view at the top needs the constraints fixed first, then the tableview.
The first black view should be pinned to the top, right and left sides with no width, and a height specified. The tableview should be 0 all the way around.
If that doesn't work, can you share the starboard file?
Related
I have tried the instructions from http://fuckingscrollviewautolayout.com/ to no avail; I have gotten everything to work but the top constraint is not behaving as expected. It says it's 0, but there's clearly a very non-zero line forcing my stackview inside the scrollview to be greatly below the top of the scrollview.
Oh wow, I finally figured it out after extensive research. I think it was because of navigation bar, but it really was extreme complicatedness/obscurity of Apple, no way to spin it otherwise.
First I read on http://commandshift.co.uk/blog/2017/01/09/scrollviews-and-stack-views/ that "The scroll view needs to be pinned to the leading and trailing margins, with a space of zero. It should also be pinned to the top and bottom of the root view (not the layout guides - you’ll need to hold alt down while choosing constraints to get this instead)." Okay, but I couldn't figure out how to hold alt down to get these results.
Finally, I dragged the scroll view all the way to the top of the entire UI preview (PAST the navigation bar). Then, I deleted the "top" constraint of the scrollview. Then, I added another constraint that the top constraint of the scrollview should be zero. Magically, this allowed me to set the top of the scrollview to the "top" rather than the "guide layout top" and this fixed my issue.
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 trying to figure out how to use autolayout, and there seems to be a lack of info about it on the internet. I placed a view within my view controller with three buttons inside it. Before I place any constraints, the buttons show up on the preview. However, as soon as I make ANY constraints, the whole sub view completely stops showing up in the preview. I made a constraint to center vertically, and then one to place it 25 pixels from the left edge. It should know exactly where to place it, but still nothing shows up.
Any ideas on why constraints cause my buttons to not show up?
With Autolayouts the constraints should be as clear as possible. You can try by adding size constraints as well as fix(right, top or bottom) constraints if necessary.
When you don't use auto-layout it positions everything manually. When you enable auto-layout it switches this behaviour off so you have an unconstrained object, which is why it dissappeared.
Turns out I didn't have constraints on the size of the sub view, so it didn't know how to center it. I didn't think I needed that because I thought it would default to what it's already at.
I am using storyboard in an iOS/xcode project with the following simplified "hierarchy" (from top to bottom):
ViewController (not a TableViewController for some customizing reasons)
View
TableView (and some other content that does not matter I think)
Cell
ContentView
"AnyView" (specific type seems to be irrelevant, as the following happens for every item I tried)
When I set the constraints between AnyView and ContentView to Zero Spacing for Top/Bottom/Leading/Trailing there is still quite some "space" between the ContentView and AnyView. (I have never encountered this when doing similar things just programmatically.)
In order to get rid of that spacing I looked for everything in storyboard options that sounded even remotely like spacing / intending etc., but did not really succeed.
By accident I just realized that when I drag & drop a view into the ContentView in a certain way it appears that storyboard sets a negative spacing of -8 that seems to "remove" that spacing. I could just use that number in my constraints, but it seems somewhat random. Does anyone know what the reason for my unwanted spacing might be or if that -8 has a deeper meaning (e.g. maybe Apple is forcing some design guidelines onto storyboard users that way?)?
Thank you very much for any insights!
In Xcode 6 constraints can be "relative to margin". The margin is set as the normal default spacing to the edge of the screen.
You can turn this off in the "add constraints" button...
Or in the constraint property inspector...
I know this may sound weird, but the only solution for me to remove top padding in UITableViewCell is to remove the existing "Top Space" constraint and add it back and setting it to 0.
When I select "Vertical Spacing" in interface builder, I expect the bottom of the upper view and the top of the lower view to have a relationship. However, frequently what seems to happen is that the bottom of the lower view gets linked to the bottom of the upper view. Then at runtime the bottom view will have the wrong frame (after resizes, animations, etc.). Is there a way I can ensure that views are consistently linked bottom to top?
[EDIT]
Here's another example. I set a height of 419px. I select "Top space to top layout guide". But instead it puts a constraint for how far the bottom of the imageview should be from the top of the superview. Then I change the height from 419px to 374px, and the view has correct height, but a gap at the top.
I've noticed this many times (usually when attaching the top of something to the topLayoutGuide), and I've put it down to a bug in Interface Builder. It seems like the issue has gone away in the betas of Xcode 5.1
The only workaround I've found is to initially move your view away from the view you're attaching it to, and then create the constraint. In this situation, Xcode will usually do the right thing. Then manually edit the constraint's constant back to 0 (or whatever you want it to be).
Instead of adding vertical spacing, try by adding "Top spacing to nearest neighbor" constraint. Please refer attached image,
Hope this will help.
This issue is fixed in the latest versions of Xcode. You can now select which part of the constraint you want to attach to. It may attach wrong initially, but the constraint editing controls are much better now.