Last active
July 29, 2017 18:07
-
-
Save anirudhamahale/74fb93685923076e6feaa05803a86c0b to your computer and use it in GitHub Desktop.
Most commonly used methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Best way to dismiss the keyboard | |
func resignKeyboard(){ | |
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, for:nil) | |
// Visit this link: http://roadfiresoftware.com/2015/01/the-easy-way-to-dismiss-the-ios-keyboard/ | |
} | |
// Used to set the status bar background color. | |
func setStatusBarBackgroundColor(color: UIColor) { | |
// Beware that this method uses the private API, your app might bet rejected from the app store. | |
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return } | |
statusBar.backgroundColor = color | |
} | |
// toggle the shadow below the navigation bar. | |
func showShadownBelow(_ navigationBar: UINavigationBar, hide: Bool) { | |
navigationBar.layer.shadowColor = UIColor.black.cgColor | |
navigationBar.layer.shadowOffset = hide ? CGSize(width: 0.0, height: 2.0) : CGSize(width: 0.0, height: 2.0) | |
navigationBar.layer.shadowRadius = hide ? 0.0 : 4.0 | |
navigationBar.layer.shadowOpacity = hide ? 0.0 : 0.5 | |
navigationBar.layer.masksToBounds = false | |
} | |
// toggle the line which is present below navigation bar. | |
func toggleLineBelow(_ navigationBar: UINavigationBar, hide: Bool) { | |
navigationBar.shadowImage.alpha = hide ? 0.0 : 1.0 | |
} | |
// This will print all the fonts that are available for the projetct including that you have added in the xcode. | |
let fontFamilyNames = UIFont.familyNames | |
for familyName in fontFamilyNames { | |
print("------------------------------") | |
print("Font Family Name = [\(familyName)]") | |
let names = UIFont.fontNames(forFamilyName: familyName) | |
print("Font Names = [\(names)]") | |
} | |
func degreesToRadians(degrees: CGFloat) -> CGFloat { | |
return degrees * CGFloat(M_PI) / 180 | |
} | |
func radiansToDegress(radians: CGFloat) -> CGFloat { | |
return radians * 180 / CGFloat(M_PI) | |
} | |
// Load image async and cache | |
// var imageCache = NSCache<AnyObject, AnyObject>() | |
func setImage(with url: URL, imageView: UIImageView, placeholderImage: UIImage, imageCache: NSCache<AnyObject, AnyObject>?, completion: @escaping (Bool)->()) { | |
imageView.image = placeholderImage | |
if let image = imageCache?.object(forKey: url as AnyObject) as? UIImage { | |
// If image is found, assign the image and return | |
imageView.image = image | |
completion(true) | |
return | |
} | |
// Image not found so download the image. | |
URLSession.shared.dataTask(with: url) { (data, response, error) in | |
if error != nil && data != nil { | |
print(error!.localizedDescription) | |
completion(false) | |
return | |
} | |
DispatchQueue.main.async { | |
let imageToCache = UIImage(data: data!) | |
self.imageCache?.setObject(imageToCache!, forKey: url as AnyObject) | |
imageView.image = imageToCache | |
completion(true) | |
} | |
}.resume() | |
} | |
*------------------------- Way to cache profile image. -------------------------* | |
// Since UIImage doesn't confirm to NSCoding protocol so we have to save UIImage as Data. | |
var profilePictureCache = settingDefaults.value(forKey: "profilePictureCache") as? Data { | |
didSet { | |
settingDefaults.setValue(profilePictureCache, forKey: "profilePictureCache") | |
} | |
} | |
func showProfilePicture(imageView: UIImageView, url: URL) { | |
imageView.image = #imageLiteral(resourceName: "ProfilePlaceholder-1") | |
if let image = profilePictureCache { | |
imageView.image = UIImage(data: image) | |
return | |
} | |
URLSession.shared.dataTask(with: url) { (data, response, error) in | |
if error != nil && data != nil { | |
print(error!.localizedDescription) | |
return | |
} | |
DispatchQueue.main.async { | |
profilePictureCache = data! | |
imageView.image = UIImage(data: data!) | |
} | |
}.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment