Skip to content

Instantly share code, notes, and snippets.

View iAmrSalman's full-sized avatar

Amr Salman iAmrSalman

View GitHub Profile
@iAmrSalman
iAmrSalman / UIImageExtension.swift
Created August 1, 2017 10:30
[UITabBarControllerExtension] #swift3 #tabbar
import UIKit
extension UIImage {
class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
@iAmrSalman
iAmrSalman / Global.swift
Created August 1, 2017 11:49
[UINavigationBar Appearance] #swift3 #navigationBar
func AV_FONT(_ size: CGFloat) -> (UIFont?) {
return UIFont(name: "Avenir-Medium", size: size)
}
@iAmrSalman
iAmrSalman / navigationBarShadow.swift
Created August 1, 2017 12:22
[NavigationBar Shadow] #swift3 #navigationbar #shadow
navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
navigationController?.navigationBar.layer.shadowRadius = 3.0
navigationController?.navigationBar.layer.shadowOpacity = 0.2
navigationController?.navigationBar.layer.masksToBounds = false
@iAmrSalman
iAmrSalman / removeTopLineTabBar.swift
Created August 1, 2017 12:57
[remove top line from TabBar] #swift3 #tabbar
UITabBar.appearance().layer.borderWidth = 0.0
UITabBar.appearance().clipsToBounds = true
@iAmrSalman
iAmrSalman / tableViewActivityIndicator.swift
Created August 3, 2017 21:59
[tableViewActivityIndicator] Add a ActivityIndicator to the bottom of UITableView while loading #swift3 #tableview #paging
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
// print("this is the last cell")
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
self.tableView.tableFooterView = spinner
@iAmrSalman
iAmrSalman / formateDate.swift
Created August 5, 2017 05:50
[formate loclized date from unix date] #swift3 #date
fileprivate func formatDate(fromUnixDate ud: Int) -> String {
let date = Date(timeIntervalSince1970: TimeInterval(ud))
let dateString = DateFormatter.localizedString(from: date, dateStyle: .medium, timeStyle: .short)
return dateString
}
@iAmrSalman
iAmrSalman / languageDirection.swift
Created August 5, 2017 05:53
[language direction] how can determine the current direction for each individual view
if #available(iOS 9.0, *) {
if UIView.userInterfaceLayoutDirection(
for: myView.semanticContentAttribute) == .rightToLeft {
// The view is shown in right-to-left mode right now.
}
} else {
// Use the previous technique
if UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft {
@iAmrSalman
iAmrSalman / localizedDateString.swift
Created August 5, 2017 07:11
[localized date string] #swift3 #date #localization
extension Date {
func localizedString() -> String {
let dateString = DateFormatter.localizedString(from: self, dateStyle: .medium, timeStyle: .short)
return dateString
}
}
@iAmrSalman
iAmrSalman / layoutCollectionViewCellSizing.swift
Created August 10, 2017 11:35
[collectionView cell sizing] #swift3 #collectionview #layout
func setupCellSizing() {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
layout.itemSize = CGSize(width: screenWidth/3, height: screenWidth/3)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView!.collectionViewLayout = layout
}
@iAmrSalman
iAmrSalman / handleOrientationChangle.md
Created August 26, 2017 20:54
[Detect orientation change] #swift3

Here's how I got it working:

In AppDelegate.swift inside the didFinishLaunchingWithOptions function I put:

NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

and then inside the AppDelegate class I put the following function: