iPhone X and bottom safe area inset - ios

I am working on layout for iPhoneX and have an issue with action sheets and Safari browser. This is an extra space. I need that my action sheets display more at the bottom ? How can I do that? I guess I need to disable safe area but how can I do it?
The reason why it happens probably that I use 2 UIWindows: one for all content and the second to display banner at the bottom of the screen.
the space that is outlined - is something to hide my app from everyone :) It does not exist in the app!
I want the action sheet be closer to the bottom. It works well on all devices except iPhoneX. I have 2 UIWindows (one for all content and the second one for ad at the bottom). So, for iPhoneX the content window has probably some safe area inset and becaise of that action sheet shows not from the bottom edge

You don't have to disable the Safe Areas for the View Controller. They are still useful for the top, trailing and leading edges.
To remove the grey bar all you need is to set the bottomAnchor or your containing view to your superView instead of the bottom safe area with a constant of what you need.
You can also further adjust safe area insets with additionalSafeAreaInsets. https://developer.apple.com/documentation/uikit/uiviewcontroller/2902284-additionalsafeareainsets That allows you to leave them as is for the edges that you don't want to modify.

Related

Auto-layout constraints to make WKWebView go full screen

I will mention right away that I'm not iOS developer and this is first time I'm messing with it because I was asked to update web application inside iOS WKWebView wrapper.
I managed to load the web app inside iOS app but on iPhone X there is always white space on top and bottom. I just learned about constraints and I heard that this is default safe area behaviour. I'm trying to expand my WKWebView past safe area to go all the way to the top notch and bottom home button so that there are no white spaces. Inside Xcode all constraints for safe area object is set equal to superview, following web view container object set equal to superview. But I still have white spaces on top and bottom.
There is also top "View" object which I think is elsewhere known as superview, which when I click on I see the constraints for it set equal to safe area and I can only keep it set to safe area or web view container which itself is set to superview. I find this very confusing. What am I doing wrong here?
I would appreciate help regarding storyboard and not code related as I don't speak Swift.
EDIT: Snapshots of constraints
Web View Container Object: https://imgur.com/GcRFeL0
If you use storyboard, you can do like this:
select this constraint and at the right panel, you should change the Second item form Safe Area to SuperView like below:
modify the Constant value from 44 to 0, like below:
the same operation to other constraints.

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.

Storyboard buttons are off-kilter

XCode 9.3: macOS 10.13.4
I have an swift app where the buttons are off-kilter. I need them at the bottom of the screen and centered. The iOS simulator for iPhone 8 shows the buttons correctly. However, the simulator for the iPhone SE and the actual SE (my iPhone) show the buttons anchored to the right edge of the screen. How can I insure that the buttons are displayed correctly on all iPhones?
First sol
this layout can be done correctly if the center of the middle button is equal to the center of the screen
Second sol
create a UIStackView with these constraints
1- left , right , bottom to screen
2- height say 100
with distribution set to fillEqually , spacing set to say 20
drag the buttons inside it and they will look right in any device
Remove the constraints from the 3 buttons: Calc, Clear, and Help
Align clear to be horizontally centered
Align Calc trailing offset from Clear's leading.
Align Helps Leading offset from Clear's trailing.
Align Clear bottom offset from the bottom margin.
Align Clear, Calc, and Help along to center vertically with one another.
You have two choices: 1) use the legacy struts and springs mode in interface builder to distribute your items as you need them or 2) spend the time learning auto layout, and use it instead. There isn't any easy answer because of all the different screen sizes, and you haven't even considered iPad screens here. The best way to pick up auto layout is to view the WWDC videos on the subject. Yes, it will take longer, but your app will respond better to screen changes, and you will also be able to inherently accommodate new screen sizes.
Use a horizontal stack view for buttons, and set spacing between buttons.
Approach 1 - Set trailing space and leading space, pin the view to bottom and set a fixed height for stack view.
Approach 2 -
Set fixed height and width to stack view and center horizontally, and pin the stack view to bottom.
Always try to use stack view, with that you will reduce the number of autolayout issues you run into.
Please add the constraint for stack view and adjust like that:

iPhone X: unsafe area doesn't get color of the nearst view

my app is built with custom top bar instead of the UINavigationBar to satisfy some requirements that cannot be done easily with the default UINavigationBar, however, after reading the answers of this question, and changing the 3 constraints of this top bar (top, leading & trailing) to be relative to the safe area instead of the superview, I couldn't get the safe area to color itself automatically to match the color of the top bar, here is what it looks like now:
the first answer in the link above suggests to manually color the status bar but unfortunately this is unreasonable and it uses a private API to do so. and the second answer suggests to provide a background color for the main view but it's also unreasonable because I need a different color for the main view.
how can I let the unsafe area to color itself according to the nearest view which is connected to it with auto layout constraints ?
Here is an example of the safe area layouts on your iPhone X.
https://1.bp.blogspot.com/-nKGb8plVgd8/Wdir-wLm6tI/AAAAAAAADmo/z3G6lWBlomASUtRJ6COYYKPdPC6KdMlBACLcBGAs/s1600/safe_area.png
If you want that your "unsafe top area" gets the same color of the nearest view color, you have two logical choices.
By using two views, one on the top with a top constraint related to the superview (according to the SafeAreaLayout activation on your storyboard) and the other just beyond the latter with its top constraint linked to the top safe area. After that, link them to your viewcontroller and manage the color into your viewDidLoad method.
The other choice is probably easier by using only one view. Set its top, leading and trailing constraints to the Superview (and not the safe area layout). To avoid an interface builder error, set a fixed height to your view.
It should work properly now.

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

Resources