Skip to content

Instantly share code, notes, and snippets.

@hisoka0917
hisoka0917 / SelfAware.swift
Created January 4, 2019 09:21
Self awake code when iOS app launch
import Foundation
import UIKit
protocol SelfAware: class {
static func awake()
}
class ModuleManagerBase {
static func registerModules() {
//let startTime = NSDate.timeIntervalSinceReferenceDate
@hisoka0917
hisoka0917 / Struct2Dictionary.swift
Last active December 23, 2022 03:54
Convert codable struct to dictionary
public protocol RawEnum {
var anyRawValue: Any { get }
}
public extension RawEnum where Self: RawRepresentable {
public var anyRawValue: Any {
get {
let mirror = Mirror(reflecting: self)
if mirror.displayStyle != .enum {
print("WARNING: You can only extend an enum with the Enum protocol")
@hisoka0917
hisoka0917 / WebViewExampleViewController.swift
Created October 11, 2018 07:13 — forked from fxm90/WebViewExampleViewController.swift
Show progress of WKWebView in UIProgressBar that is attached to an UINavigationBar
//
// WebViewExampleViewController.swift
//
// Created by Felix Mau on 06.01.18.
// Copyright © 2018 Felix Mau. All rights reserved.
//
import UIKit
import WebKit
@hisoka0917
hisoka0917 / UIView+AnchorPoint.swift
Created July 13, 2018 06:57
Change UIView anchor point in Swift
extension UIView {
func setAnchorPoint(_ point: CGPoint) {
var newPoint = CGPoint(x: bounds.size.width * point.x, y: bounds.size.height * point.y)
var oldPoint = CGPoint(x: bounds.size.width * layer.anchorPoint.x, y: bounds.size.height * layer.anchorPoint.y)
newPoint = newPoint.applying(transform)
oldPoint = oldPoint.applying(transform)
var position = layer.position
@hisoka0917
hisoka0917 / UIStackView+ReverseSubviews.swift
Last active July 14, 2022 11:15
Reverse UIStackView Arranged Subviews in Z index
extension UIStackView {
func reverseSubviewsZIndex(setNeedsLayout: Bool = true) {
let stackedViews = self.arrangedSubviews
stackedViews.forEach {
self.removeArrangedSubview($0)
$0.removeFromSuperview()
}
@hisoka0917
hisoka0917 / UITableViewController+FixBottomBar.swift
Created June 1, 2018 09:43
UITableViewController with a fixed bottom button
class MyTableViewController: UITableViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
bottomButton.size = CGSize(width: self.view.width, height: 50)
bottomButton.translatesAutoresizingMaskIntoConstraints = false
bottomButton.addTarget(self, action: #selector(bottomButtonClick), for: .touchUpInside)
self.navigationController?.setToolbarHidden(false, animated: animated)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
@hisoka0917
hisoka0917 / String+SHA256.swift
Created April 26, 2018 08:39
Swift String SHA256
extentsion String {
func SHA256() -> String {
if let stringData = self.data(using: .utf8) {
return self.hexStringFromData(input: digest(input: stringData as NSData))
} else {
return self
}
}
@hisoka0917
hisoka0917 / UIButton+BackgroundColorForState.swift
Created April 8, 2018 08:39
UIButton setBackgroundColor for state in Swift 4
extension UIButton {
func setBackgroundColor(_ color: UIColor, for state: UIControlState) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
if let currentGraphicsContext = UIGraphicsGetCurrentContext() {
currentGraphicsContext.setFillColor(color.cgColor)
currentGraphicsContext.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
}
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, for: state)
@hisoka0917
hisoka0917 / Dictionary+UrlQuery.swift
Created February 6, 2018 02:58
Format Dictionary to url query
extension Dictionary {
func urlQueryEncode() -> String {
return self
.map({ "\($0)=\($1)" })
.joined(separator: "&")
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
}
func queryParameters() -> String {
let parameterArray = self.map { (key, value) -> String in