iOS Regular Codes
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 } } |
[…]