Hide keyboard on the outside click iOS
1 2 3 |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) //This will hide the keyboard } |
How to hide Navigation Bar without losing slide-back ability
Extend: UIGestureRecognizerDelegate
1 2 3 |
self.navigationController?.isNavigationBarHidden = true self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true self.navigationController?.interactivePopGestureRecognizer?.delegate = self |
Estimate the size of the cell according to text
1 2 3 4 5 6 7 8 |
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let cgsiz = CGSize(width: self.view.frame.width , height:1000) let att = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)] let estFram = NSString(string: self.tableData[indexPath.row].value).boundingRect(with: cgsiz, options: .usesLineFragmentOrigin, attributes: att, context: nil) let height = estFram.height + 40 return height } |
Change UIPageControl button on scroll
Layout: Flow
Scroll Direction: Horizontal
Scrolling enabled
Paging enabled
1 2 3 4 5 6 7 8 9 10 11 12 |
@IBOutlet weak var pageController: UIPageControl! ... self.pageController.numberOfPages = self.imagesArray.count ... func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { pageController?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width) } func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { pageController?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width) } |
ios date
1 2 3 4 |
let tranDate:Date = Date() let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let tranDateResult = formatter.string(from: tranDate) |
Convert date to today or time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public static func dayDifference(from interval : TimeInterval) -> String { let calendar = NSCalendar.current let date = Date(timeIntervalSince1970: interval) if calendar.isDateInYesterday(date) { return "Yesterday" } else if calendar.isDateInToday(date) { let unixTimestamp = TimeInterval.self print(unixTimestamp) let date = Date(timeIntervalSince1970: interval) let dateFormatter = DateFormatter() // dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want dateFormatter.locale = NSLocale.current dateFormatter.dateFormat = "hh:mm a" //Specify your format that you want let strDate = dateFormatter.string(from: date) print(strDate) return strDate } else { let unixTimestamp = interval let date = Date(timeIntervalSince1970: unixTimestamp) let dateFormatter = DateFormatter() // dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want dateFormatter.locale = NSLocale.current dateFormatter.dateFormat = "dd/MM" //Specify your format that you want let strDate = dateFormatter.string(from: date) return strDate } } |
String to md5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import CommonCrypto static func strToMD5(string: String) -> Data { let messageData = string.data(using:.utf8)! var digestData = Data(count: Int(CC_MD5_DIGEST_LENGTH)) _ = digestData.withUnsafeMutableBytes {digestBytes in messageData.withUnsafeBytes {messageBytes in CC_MD5(messageBytes, CC_LONG(messageData.count), digestBytes) } } return digestData } // use // let md5Hex = strToMD5(string: url).map { String(format: "%02hhx", $0) }.joined() |
Share test/url
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@IBAction func didTapShareCode(_ sender: Any) { let someText:String = "Check out that aussom app" let objectsToShare:URL = URL(string: "https://itunes.apple.com/in/app/bargain-builder/id1447928833?mt=8")! let sharedObjects:[AnyObject] = [objectsToShare as AnyObject,someText as AnyObject] let activityViewController = UIActivityViewController(activityItems : sharedObjects, applicationActivities: nil) activityViewController.popoverPresentationController?.sourceView = self.view // activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook,UIActivity.ActivityType.postToTwitter,UIActivity.ActivityType.mail] activityViewController.excludedActivityTypes = [UIActivity.ActivityType.postToFacebook,UIActivity.ActivityType.postToTwitter] self.present(activityViewController, animated: true, completion: nil) } |
Random Color List
1 2 3 4 5 6 7 8 9 10 |
extension UIColor { static func random() -> UIColor { let r:CGFloat = CGFloat(arc4random_uniform(255)) / 255.0 let g:CGFloat = CGFloat(arc4random_uniform(255)) / 255.0 let b:CGFloat = CGFloat(arc4random_uniform(255)) / 255.0 return UIColor(red: r, green: g, blue: b, alpha: 1.0) } } |
Show Alert
1 2 3 4 5 |
//Alert Message Code let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil)) self.present(alert, animated: true, completion: nil) //End Alert Message Code |
ref: https://stackoverflow.com/questions/24022479/how-would-i-create-a-uialertview-in-swift
Show Image from Url
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
func showImageFromUrl(ImageUrl: String ){ if let url = URL(string: ImageUrl) { imageView.contentMode = .scaleAspectFit downloadImage(url: url) } } func downloadImage(url: URL) { print("Download Started") getDataFromUrl(url: url) { data, response, error in guard let data = data, error == nil else { return } print(response?.suggestedFilename ?? url.lastPathComponent) print("Download Finished") DispatchQueue.main.async() { self.imageView.image = UIImage(data: data) } } } func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) { URLSession.shared.dataTask(with: url) { data, response, error in completion(data, response, error) }.resume() } |
Pass data in between controllers
1 2 3 4 5 6 7 8 9 |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { //check which segue we wants to target if(segue.identifier == "MobileConsumptionSegue"){ // make object of that segue let mobileConsumptionViewController:MobileConsumptionViewController = segue.destination as! MobileConsumptionViewController //change a field we wants to change mobileConsumptionViewController.receivedData = 1279381674 } } |