Skip to content

Instantly share code, notes, and snippets.

View damirstuhec's full-sized avatar
💭
👨‍💻

Damir Stuhec damirstuhec

💭
👨‍💻
View GitHub Profile
@ole
ole / RelativeSizeLayout.swift
Last active March 24, 2024 22:24
A SwiftUI layout and modifier for working with relative sizes ("50 % of your container"). https://oleb.net/2023/swiftui-relative-size/
import SwiftUI
extension View {
/// Proposes a percentage of its received proposed size to `self`.
///
/// This modifier multiplies the proposed size it receives from its parent
/// with the given factors for width and height.
///
/// If the parent proposes `nil` or `.infinity` to us in any dimension,
/// we’ll forward these values to our child view unchanged.
@chriseidhof
chriseidhof / ViewToPDF.swift
Created January 6, 2023 15:46
Image Rendering
import SwiftUI
extension View {
@MainActor
func pdf(size: ProposedViewSize) -> Data {
let renderer = ImageRenderer(content: self)
renderer.proposedSize = size
var pdfData = NSMutableData()
renderer.render { size, render in
var mediaBox = CGRect(origin: .zero, size: size)
import SwiftUI
@main
struct MenuBarApp: App {
@NSApplicationDelegateAdaptor(StatusBarDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
@brettohland
brettohland / 1.0 README.md
Last active July 21, 2023 17:09
ParseableFormatStyle Examples
import SwiftUI
struct ContentView: View {
@State var cond = false
var body: some View {
ZStack {
Color.blue
Test(cond: cond).onTapGesture {
withAnimation(.easeInOut(duration: 2)) {
cond.toggle()
@tgrapperon
tgrapperon / JuxtaposedView.swift
Last active November 16, 2022 17:21
SwiftUI modifier to juxtapose a view to a root view without affecting the layout of the root view.
import SwiftUI
public extension View {
func juxtapose<Content>(
edge: Edge = .top,
spacing: CGFloat = 8,
@ViewBuilder content: @escaping () -> Content
) -> some View where Content: View {
modifier(
JuxtaposedViewModifier(
@kylehughes
kylehughes / HapticFeedback.swift
Last active May 17, 2022 15:46
Convenient Swift abstractions for generating haptic feedback on iOS.
// Copyright 2021 Kyle Hughes
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
@davidsteppenbeck
davidsteppenbeck / Optional+Utilities.swift
Last active July 27, 2021 07:08
An infix operator for collections that returns the wrapped value of the optional only if the collection is not empty.
import Foundation
infix operator ???
extension Optional where Wrapped: Collection {
/// Performs a nil-coalescing operation, returning the wrapped value of an `Optional` instance
/// only if the wrapped value is not empty, otherwise returns a default value.
///
/// - Parameters:
@dive
dive / xed_xcode_invocation_tool.md
Last active July 27, 2023 15:23
Xcode invocation tool - xed

Xcode invocation tool - xed

xed is a command-line tool that launches the Xcode application and opens the given documents (xcodeproj, xcworkspace, etc.), or opens a new document, optionally with the contents of standard input.

If you work from the command line, this tool is a better option than open (which can open Xcode projects as well). Why?

  • xed knows about the current selected Xcode version (open behaves unpredictably if you have multiple Xcode installed)
  • You can use it open all files from a specific commit (with a little help explained below). It is useful on code-reviews or when you want to explore significant changes in the repository
  • You can use it as a "quick open" helper. Helps with monorepo phenomena, when you have hundreds of projects in the repository (I will show you an example below)
@shaundon
shaundon / ContentView.swift
Last active July 4, 2022 11:31
PHPicker in SwiftUI
import SwiftUI
struct ContentView: View {
@State private var showPhotoSheet = false
@State private var image: UIImage? = nil
var body: some View {
VStack {
Button(action: { showPhotoSheet = true }) {
Label("Choose photo", systemImage: "photo.fill")