84 lines
2.8 KiB
Swift
84 lines
2.8 KiB
Swift
//
|
|
// BrowserView.swift
|
|
// SBrowser
|
|
//
|
|
// Created by James Magahern on 7/21/20.
|
|
//
|
|
|
|
import Combine
|
|
import UIKit
|
|
import WebKit
|
|
|
|
class BrowserView: UIView
|
|
{
|
|
let titlebarView = TitlebarView()
|
|
|
|
var toolbarView: ToolbarView? {
|
|
didSet { addSubview(toolbarView!) }
|
|
}
|
|
|
|
var webView: WKWebView? {
|
|
didSet {
|
|
if let toolbarView = toolbarView {
|
|
insertSubview(webView!, belowSubview: toolbarView)
|
|
} else {
|
|
addSubview(webView!)
|
|
}
|
|
}
|
|
}
|
|
|
|
var keyboardWillShowObserver: AnyCancellable?
|
|
var keyboardWillHideObserver: AnyCancellable?
|
|
var keyboardLayoutOffset: CGFloat = 0 { didSet { setNeedsLayout() } }
|
|
|
|
convenience init() {
|
|
self.init(frame: .zero)
|
|
|
|
addSubview(titlebarView)
|
|
|
|
keyboardWillShowObserver = NotificationCenter.default.publisher(for: UIWindow.keyboardWillShowNotification).sink { notification in
|
|
if let keyboardFrame = notification.userInfo?[UIWindow.keyboardFrameEndUserInfoKey] as? CGRect {
|
|
self.keyboardLayoutOffset = self.bounds.height - keyboardFrame.minY
|
|
}
|
|
}
|
|
|
|
keyboardWillHideObserver = NotificationCenter.default.publisher(for: UIWindow.keyboardWillHideNotification).sink { notification in
|
|
if let keyboardFrame = notification.userInfo?[UIWindow.keyboardFrameEndUserInfoKey] as? CGRect {
|
|
self.keyboardLayoutOffset = self.bounds.height - keyboardFrame.minY
|
|
}
|
|
}
|
|
}
|
|
|
|
override func layoutSubviews()
|
|
{
|
|
super.layoutSubviews()
|
|
webView?.frame = bounds
|
|
|
|
if let toolbarView = toolbarView {
|
|
var toolbarSize = toolbarView.sizeThatFits(bounds.size)
|
|
if keyboardLayoutOffset == 0 {
|
|
toolbarSize.height += safeAreaInsets.bottom
|
|
}
|
|
|
|
toolbarView.bounds = CGRect(origin: .zero, size: toolbarSize)
|
|
toolbarView.center = CGPoint(x: bounds.center.x, y: bounds.maxY - (toolbarView.bounds.height / 2) - keyboardLayoutOffset)
|
|
}
|
|
|
|
bringSubviewToFront(titlebarView)
|
|
|
|
var titlebarHeight: CGFloat = 24.0
|
|
titlebarHeight += safeAreaInsets.top
|
|
|
|
titlebarView.frame = CGRect(origin: .zero, size: CGSize(width: bounds.width, height: titlebarHeight))
|
|
|
|
// Fix web view content insets
|
|
webView?.scrollView.automaticallyAdjustsScrollIndicatorInsets = false
|
|
|
|
var webViewContentInset = UIEdgeInsets()
|
|
webViewContentInset.top = titlebarView.frame.height
|
|
webViewContentInset.bottom = toolbarView?.frame.height ?? 0
|
|
webView?.scrollView.contentInset = webViewContentInset
|
|
webView?.scrollView.scrollIndicatorInsets = webViewContentInset
|
|
}
|
|
}
|