Skip to content

Instantly share code, notes, and snippets.

View m760622's full-sized avatar

Mohammed Abunada m760622

View GitHub Profile
@tfrank64
tfrank64 / AppDelegate.swift
Created June 8, 2014 17:32
Swift UITabBar Programmatically
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav1 = UINavigationController()
var first = FirstViewController(nibName: nil, bundle: nil)
nav1.viewControllers = [first]
var second = SecondViewController(nibName: nil, bundle: nil)
var nav2 = UINavigationController()
@dduan
dduan / UISegmentedControl+VerticalLayout.swift
Last active July 25, 2024 05:52
Turns a UISegmentedControl into a vertical layout.
import UIKit
extension UISegmentedControl {
func goVertical() {
self.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
for segment in self.subviews {
for segmentSubview in segment.subviews {
if segmentSubview is UILabel {
(segmentSubview as UILabel).transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
@tempire
tempire / array_modification.swift
Last active December 1, 2020 06:18
swift array modification in loop
// FAILURE
var array = [["name": "glen"]]
for item in array {
item["rank"] = "advanced" // Generates an @lvalue error
}
// Even though array is immutable, it's of type <Array<Dictionary<String,String>>,
// item is assigned by let automatically.
var startNumber = 240
var timer = Timer()
func runTime() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController.tTimer)), userInfo: nil, repeats: true)
}
@objc func tTimer() {
if startNumber > 0 {
startNumber -= 1
numberLabel.text = String(startNumber)
@Deub27
Deub27 / UIStackView+removeAll.swift
Created November 25, 2017 14:00
Remove all arranged subviews from UIStackView at once
import UIKit
extension UIStackView {
func removeAllArrangedSubviews() {
let removedSubviews = arrangedSubviews.reduce([]) { (allSubviews, subview) -> [UIView] in
self.removeArrangedSubview(subview)
return allSubviews + [subview]
}
@bantic
bantic / upload.swift
Created December 9, 2017 19:10
upload UIImage to server with swift
func saveImage(image: UIImage, name:String) {
let req = NSMutableURLRequest(url: NSURL(string:"http://127.0.0.1:3001/")! as URL)
let ses = URLSession.shared
req.httpMethod="POST"
req.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
req.setValue(name, forHTTPHeaderField: "X-FileName")
let jpgData = UIImageJPEGRepresentation(image, 1.0)
req.httpBodyStream = InputStream(data: jpgData!)
let task = ses.uploadTask(withStreamedRequest: req as URLRequest)
task.resume()
@benasher44
benasher44 / Swift42Compatibility.swift
Last active March 3, 2019 23:03
Swift 4.2 Compatibility Shims
//
// Created by Ben Asher on 6/13/18.
// Copyright © 2018-present PlanGrid. All rights reserved.
//
import Foundation
import MapKit
import UserNotifications
#if swift(>=4.2)
@th3m477
th3m477 / NSLayoutAnchor+Shortcuts.swift
Created August 2, 2019 07:35
Constraint shortcuts
extension NSLayoutAnchor {
@objc @discardableResult func eq(_ anchor: NSLayoutAnchor, _ constant: CGFloat = 0.0) -> NSLayoutConstraint {
let c = constraint(equalTo: anchor, constant: constant)
c.isActive = true
return c
}
@objc @discardableResult func lte(_ anchor: NSLayoutAnchor, _ constant: CGFloat = 0.0) -> NSLayoutConstraint {
let c = constraint(lessThanOrEqualTo: anchor, constant: constant)
c.isActive = true
@Pobe16
Pobe16 / YouTubeView.swift
Last active May 24, 2022 21:57
YouTubeView UIViewRepresentable
import SwiftUI
import UIKit
import YouTubePlayer
final class YouTubeView: UIViewRepresentable {
typealias UIViewType = YouTubePlayerView
@ObservedObject var playerState: YouTubeControlState