Skip to content

Instantly share code, notes, and snippets.

View megabitsenmzq's full-sized avatar
🍤
金鱼不可以吃哦!

Jinyu Meng megabitsenmzq

🍤
金鱼不可以吃哦!
View GitHub Profile
@megabitsenmzq
megabitsenmzq / UIAlertControllerWrapper.swift
Last active August 25, 2023 10:04
Show UIKit alert in SwiftUI.
// SwiftUI alert has so many problems. Use UIKit instead.
import UIKit
struct UIAlertControllerWrapper {
static func present(alert: UIAlertController) {
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene, let root = scene.windows.first?.rootViewController {
var nextViewController = root
while nextViewController.presentedViewController != nil {
nextViewController = nextViewController.presentedViewController!
@megabitsenmzq
megabitsenmzq / SavedInDefaultsMacro.swift
Last active July 26, 2023 17:49
A macro that saves the variable to user defaults.
// Usage
@SavedInDefaults(defaultValue: "foo", group: "group.foo", namespace: "bar")
var fooBar: String
// Expanded ---
{
get {
if let savedValue = UserDefaults(suiteName: "group.foo")!.object(forKey: "bar.fooBar") as? String {
return savedValue
} else {
return "foo"
@megabitsenmzq
megabitsenmzq / ContentView.swift
Last active March 20, 2024 16:21 — forked from Hiroki-Kawakami/ContentView.swift
SwiftUI Change Status Bar Color with UIWindow
//
// ContentView.swift
// StatusBarTest
//
// Created by hiroki on 2021/02/11.
//
import SwiftUI
struct ContentView: View {
@megabitsenmzq
megabitsenmzq / Calc.swift
Last active August 9, 2024 14:06
Swift algebra calculator with binary tree.
import Foundation
class Calculator {
enum Operator: String, CaseIterable {
case add = "+"
case subtract = "-"
case multiply = "*"
case divide = "/"
case power = "^"
@megabitsenmzq
megabitsenmzq / View+OverCurrentContext.swift
Last active November 7, 2023 10:58
Presents a modal view with the "over current context" style in SwiftUI.
import UIKit
import SwiftUI
fileprivate var currentOverCurrentContextUIHost: UIHostingController<AnyView>? = nil
extension View {
public func overCurrentContext(
isPresented: Binding<Bool>,
showWithAnimation: Bool = false,
@megabitsenmzq
megabitsenmzq / iTunes_JP.src
Created July 13, 2020 14:35
iTunes Store metadata plugin for mp3tag (JP store as default).
########################################################
# iTunes API Sources Script for Mp3Tag #
# #
#version 1.1 #
#created by Megabits in 2019 #
########################################################
########################################################
# SEARCH BY DIALOG #
########################################################
@megabitsenmzq
megabitsenmzq / SwiftUIAlertView.swift
Last active January 25, 2021 15:00
A simple alert view in SwiftUI.
import SwiftUI
struct PopoutAlertView: View {
let lightTapticGenerator = UIImpactFeedbackGenerator(style:.light)
@State private var isHidden = true
var title = "Alert"
var content = "Content here"
var tintColor = UIColor.red