0

I want to set a safety zone by device. My web view is set to SafeArea above, SuperView below. After the iPhone X series, the value of Y is 44 because of the notch area. But for the iPhone 6 and iPhone 8, 44 is too wide. How do I adjust it?

Area of the current WKWebView

enter image description here

Load WebView Area from Current Swift5

@IBOutlet var WKWebView: FullScreenWKWebView!
...
        let config = WKWebViewConfiguration()

        contentController.add(self, name: "goApp")

        config.userContentController = contentController

        WKWebView = FullScreenWKWebView(frame: WKWebView.frame, configuration: config)

        WKWebView.uiDelegate = self
        WKWebView.navigationDelegate = self
        WKWebView.scrollView.delegate = self
        view.addSubview(WKWebView)
        view.addSubview(indicator)
...
class FullScreenWKWebView: WKWebView {
    override var safeAreaInsets: UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}

I want to set the Top SafeArea area by device.

I am trying things, but is unsuccessful.

I've set constraints, but the screens on the right and below are cut, and the top is still too wide for the iPhone 6.

enter image description here

Top part of iPhone 6

enter image description here

iosbegindevel
  • 307
  • 5
  • 20

2 Answers2

0

Make the constraint anchored to the safe area and give it the desired value. It will automatically adjust to all devices.

Here you can see a UIView with blue background, anchored top and bottom to the safe area with a 0 value (you can increase the value if you want more distance to the safe area).

Storyboard View constraints


The view will be automatically drawn giving the proper distance to the safe area (which is 44 for iPhone X or above, and 0 for iPhone 8 and below).

iPhone X result iPhone 8 result

IloneSP
  • 429
  • 2
  • 13
0

I cannot set the top area of WKWebView as a safety zone unconditionally. Because the safety area includes the NavigationController Bar Area. But I don't use the NavigationController. Therefore, it is not possible to set the safety zone as a constraint.

So I solved the problem by separating by device size.

Set Main Story Board

enter image description here

Adjusting WKWebVIew's Height and Y-Values according to Device Height

let screenHeight = UIScreen.main.bounds.size.height
if screenHeight > 667 {
            WKWebView = FullScreenWKWebView(frame: CGRect( x: 0, y: 44, width: WKWebView.frame.width, height: WKWebView.frame.height - 44 ), configuration: config)
        } else {
            WKWebView = FullScreenWKWebView(frame: CGRect( x: 0, y: 20, width: WKWebView.frame.width, height: WKWebView.frame.height - 20 ), configuration: config)
        }

iPhone 6

enter image description here

iPhone X and after

enter image description here

iosbegindevel
  • 307
  • 5
  • 20