Skip to content

Instantly share code, notes, and snippets.

View nurtugan's full-sized avatar
🏠
Working from home

Nurtugan Nuraly nurtugan

🏠
Working from home
View GitHub Profile
@nurtugan
nurtugan / UIColor+.swift
Created March 8, 2020 14:52
Dark mode color adapter
extension UIColor {
static func adapt(lightModeColor: UIColor?, darkModeColor: UIColor?) -> UIColor {
guard let lightModeColor = lightModeColor,
let darkModeColor = darkModeColor else {
fatalError("No color")
}
if #available(iOS 13, *) {
return UIColor { (traitCollection: UITraitCollection) -> UIColor in
if traitCollection.userInterfaceStyle == .dark {
/// Return the color for Dark Mode
@nurtugan
nurtugan / RemoveNavigationBarBackBarButtonItemTitle.swift
Created March 10, 2020 05:01
Removes NavigationBar's BackBarButtonItem Title
extension UIViewController {
func removeNavigationBarBackBarButtonItemTitle() {
let backButton = UIBarButtonItem()
backButton.title = ""
navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
}
}
@nurtugan
nurtugan / SafeSubscriptingOfCollection.swift
Created March 11, 2020 09:53
Safe subscripting of Collection
extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
}
@nurtugan
nurtugan / UIImageView+Kingfisher.swift
Last active April 18, 2020 10:54
Kingfisher configs
import Kingfisher
import UIKit
extension UIImageView {
static func clearImageCache() {
KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
}
/// UIImageView+: Method for downloading image
@nurtugan
nurtugan / AppearanceManager.swift
Last active March 29, 2020 17:46
My Default Appearance Manager
import UIKit
final class AppearanceManager {
static let shared = AppearanceManager()
private init () {}
func setDefaultAppearance() {
// Removes all navigation bar back button title
UIBarButtonItem.appearance().setTitleTextAttributes(
@nurtugan
nurtugan / TextFieldSwitching.swift
Last active April 18, 2020 10:53
Switching between Text Fields on pressing return key
extension MyViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Try to find next responder
if let nextTextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
nextTextField.becomeFirstResponder()
} else {
// Not found, so remove keyboard
textField.resignFirstResponder()
}
// Do not add a line break
@nurtugan
nurtugan / DismissKeyboard.swift
Created March 29, 2020 18:55
Dismiss Keyboard by touching anywhere
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(
target: self,
action: #selector(dismissKeyboard)
)
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@nurtugan
nurtugan / .swiftlint.yml
Created April 18, 2020 11:50
My SwiftLint Rules
disabled_rules:
excluded:
- Pods
- ProjectX/Supporting Files/R.generated.swift # R.swift
opt_in_rules:
- anyobject_protocol
- array_init
- attributes
@nurtugan
nurtugan / UIView+Layout.swift
Created May 8, 2020 20:20
Making Programmatic Auto Layout Easy through Extensions
//
// UIView+Layout.swift
// Project X
//
// Created by Brian Voong on 2/10/19.
// Copyright © 2019 Brian Voong. All rights reserved.
//
import UIKit
@nurtugan
nurtugan / DiffableDataSource-DeleteItem.swift
Last active July 17, 2024 20:28
Example of Deleting Item from Diffable Data Source
@objc
private func handleGet(_ sender: UIButton) {
var superview = sender.superview
while superview != nil {
if let cell = superview as? UICollectionViewCell {
guard let indexPath = collectionView.indexPath(for: cell),
let objectIClickedOnto = diffableDataSource.itemIdentifier(for: indexPath) else { return }
var snapshot = diffableDataSource.snapshot()
snapshot.deleteItems([objectIClickedOnto])
diffableDataSource.apply(snapshot)