MapKit auto layout constraints issue - ios

I added a new MKMapView onto the ViewController, and set the constraints to 0 using the "Add New Constraints" button. Now, the map view has constraints to the safe area on all four sides with value 0, but there are a few issues:
the compiler is throwing a warning saying "Height is ambiguous", even though all four sides are constrained to 0 to the safe area.
After I set the constraints, when I switch "View as:" to another device, the mapView doesn't change size in accordance with the constraints (0 to safe area, aka full screen)
On iPhone X, with the constraints set, there are white patches on the top and bottom of the screen, when what I want is to have the mapKit take the entire screen (pic below). How can I do this?

I experience the same bugginess in the interface builder as you describe in question 1 and 2 from time to time, and I can easily reproduce it now by adding a map view to an empty view, adding top/bottom/leading/trailing-to safe view constraints and switching the view modes around (different devices, landscape portrait).
The only solution I know of is to move the view manually back to where it is supposed to be. The constraint warnings will then disappear.
The issue in your last question is easy to fix though. Select your parent view and uncheck Safe Area Layout Guide on the right hand tool area.
You have to redo your constraints after this, as the safe area you constrained to before is no longer there.
Here are the results with this option checked and unchecked:

Related

General approach for bottom layout guide and different screen formats

So far most, if not all, screens i had developed had 2 kind of behaviours in the context of bottom layout guide:
The content is static and need to be anchored at the bottom:
In this case typically i create a bottom layout guide from the bottom-est vc subview to it's superview (the vc root view, in place of the vc bottom safe area). And this works in both cases if the background color of the bottom aligned view and the root/parent view in the edge of the iphoneX is the same.
the content is scrollable or has to "overflow" or the background color of bottom subview and vc differ. For example a bottom button or tabbar will have to be aligned to the safe area, but their backgrounds will have to "continue" in the iPhoneX to the non-safe-area.
In this case i either add a edge-inset/margin to the views that match the unsafe area (leading to an additional margin to normal squared screens) or i will have to adjust the constraint based on the device type.
In the light of these, is anybody aware of any technique to at generalize/simplify the 2nd point? If find it really ugly to have to add device dependent checks for almost every view controller.
This technically happens already with the "automatically adjust view controller edge insets" but only for scrollviews/tableviews.
TLDR; Is it possible to let a view "stretch" to the non-safe-area in the iphoneX? To obtain basically the same behaviour of the NavigationBar but for the bottom of the screen, without hardcoding anything?
Plus: if it is possible to generalize also including the 1st wanted behaviour with a flag or alike.
The way I typically do this:
Setup the bottom constraint like you have posted
Setup the top constraint relative to the bottom safe area (instead of the superview) with an offset for the height you want the view to be.
This way if there is safe area the view will stretch down naturally and retain its normal appearance on non-safe area phones, you just have to make sure to layout the contents of that view to stay between the safe area and the top. No device-specific code needed.

How to remove whitespace and extend background to full screen in Xcode?

I'm trying to create a simple game, but the background won't extend to the edges of any of the displays (iPhone and iPad) so there's whitespace at the top and bottom. In landscape orientation there's even more whitespace for some reason. I haven't coded anything yet.
Here's how it looks: https://imgur.com/a/pDupTOD
I have constraints added to all four sides of the Image View and them set to View and 0.
I have also tried unchecking "Use Safe Area Layout Guides" and "Safe Area Relative Margins" neither of which helped.
You have set the constraint to safe area. Instead of pinning your view's edges to safe area, pin them to your superview.
Here is how your constraints may look
Select each one of your constraint(double click on it)
And change the following, i.e. from safe area to superview. Also update the constant and set to 0.
Hope that helps

iPhone X - Safe Area does not achieve full-screen experience?

The new HIG for iPhone X available here, specify:
"Provide a full-screen experience. Make sure backgrounds extend to the edges of the display, and that vertically scrollable layouts, like tables and collections, continue all the way to the bottom."
Now I'd like to understand how to accomplish that with Xcode 9 GM, since it seems to me that the only view allowed to extend to the whole screen is the UIViewController root view, and that whenever I try to drag constraints for a view above that, i.e. WKWebView to the root view, the constraints get actually connected to the safe area, leaving both the top and bottom areas empty as displayed in the storyboard here:
Please note the answer here specifies to use the safe area, but that doesn't work because using it results in the following simulator result where WKWebView is NOT extending to the edges of the screen:
Just change your bottom constraint First Item to SuperView
Actually I believe the answer is this: to accomplish full screen we should NOT use safe area support. After I unchecked Safe Area Relative Margins and Safe Area Layout Guide on the view in Interface Builder Size Inspector I got the expected result:
In fact I would say that the idea of Safe Area is that of an area which will for sure not be overlapped by any of the system icons, or rounded borders, full screen is the opposite of it.
This answer is simple and worked for me .. follow these sequences:
1- select the background image and open constraints window
2- uncheck constrain to margins
3- as in picture : select (View) not (safe area) from the small arrow in the corner of edit value rectangle .. apply this for all 4 values
4 - enter value 0 for all 4 values and hit Enter
and Done
I solved problem by setting top and bottom like this. xcode version is 11.6

UICollectionView change flow direction on rotation

Hey, I'd like to obtain what you see in the pictures: in Compact Height mode (landscape iphone) both the red and the blue view have to take all screen vertically and half the screen horizontally. In Compact Width mode (portrait iphone)they have to take all the screen horizontally and half the screen vertically. Space between views should be same size in both modes.
I used to think I have to use size classes and auto-layout constraints, but everything I tried failed miserably.
Maybe I have to use a UICollectionView and change flow direction based on orientation (if that is even possible)?
A collection view is probably overkill, because you don't want scrolling and that's the whole point of a collection view--by the time you do the sizing to stop it you'll have done all the work necessary to set a non-scrolling layout.
This is possible with Size Classes in IB. First, In general you will probably find it helpful to name the views in the Document Outline on the left in IB. You will also want to use this outline rather than try to grab the tiny constraint H-lines.
Set up all the constraints except 1) constraints linking the
OrangeView and BlueView to each other, 2) the constraints linking
the OrangeView to the top and left(leading), and 3) The constraints
linking the BlueView to the bottom and right (trailing).
Change the size class setting at the bottom to w-Compact and
h-Any in the funky box system. Now we're designing for a compact width, so views on top of each other.
Create a constraints for vertical space for BlueView.bottom to
OrangeView.Top. Also create constraint for OrangeView to
superview.leading (or ledaing,margin) and BlueView to
superview.trailing.margin. If you select any one of these constraints and look at the Size Inspector on the right (the ruler) you should see an "installed" checkbox not selected, and below that a w-Compact h-Any and another installed box, this one selected.
Now, while keeping the constraint selected just to see what happens, change the sizeClass selector at the bottom to w-Regular h-Any. Notice that in the Document Outline to the left, it should get grayed out.
Now we are designing for regular, so side-to-side. Add constraints linking the views for horizontal space, BlueView.trailing to OrangeView.leading. Also link OrangeView.top to the superview.top or top aligned to BlueView.top, and same for bottoms. You can manually edit the frame first; if not, IB will automatically fill in the wrong values, so edit these after you create them, and verify they are w-Regular and h-Any. With the ViewController selected, select "update frames" and the views should snap to their expected shape for the size class.
Let us know if this works for you or if it was unclear. Good luck!

Autolayout - Why does "Vertical Spacing" sometimes attach to the opposite side of a view?

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.

Resources