Skip to content

Instantly share code, notes, and snippets.

Avatar

Philipp Kuecuekyan pkuecuekyan

View GitHub Profile
@pkuecuekyan
pkuecuekyan / Parser.swift
Created Feb 9, 2019
Parser — a readable and generic μ snippet for JSON/Codable parsing
View Parser.swift
//
// Parser
//
//
struct Parser<T: Decodable> {
static func parse(_ data: Data) -> T? {
var parsedPayload: T?
do {
parsedPayload = try JSONDecoder().decode(T.self, from: data)
@pkuecuekyan
pkuecuekyan / XCUIElement+ClearText.swift
Created Sep 11, 2018
Clearing text and adding enter for an input UI field (UISearchBar e.g.), for UIViewController testing
View XCUIElement+ClearText.swift
extension XCUIElement {
func clearText() {
//
// cf. and tip courtesy of
// https://stackoverflow.com/questions/32821880/ui-test-deleting-text-in-text-field
//
guard let stringValue = self.value as? String else {
@pkuecuekyan
pkuecuekyan / WKWebViewSizeToFit.swift
Last active Jun 4, 2021
Adjust height of WKWebView frame based on scrollHeight of the webView's content
View WKWebViewSizeToFit.swift
// Since the WKWebView has no sizeToFit() method, increase the frame height of the webView to
// match the height of the content's scrollHeight
//
// The WKWebView's `navigationDelegate` property needs to be set for the delegate method to be called
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
if let height = result as? CGFloat {
@pkuecuekyan
pkuecuekyan / SearchContainerInTabBar.swift
Last active Mar 23, 2021
Set up a UISearchContainerViewController inside a UITabBar for tvOS 9
View SearchContainerInTabBar.swift
func setupTabBarWithSearchContainerView() {
// configure a UIViewController to display search results
// needs to conform to the UISearchResultsUpdating protocol
let searchResultsViewController = SearchResultsViewController()
// setup UISearchController and hook up to search results UIViewController
let searchController = UISearchController(searchResultsController: searchResultsViewController)
searchController.searchResultsUpdater = searchResultsViewController
searchController.hidesNavigationBarDuringPresentation = false