#This simple snippet shows how to add a subview to superview's center.
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
class func getCategories() -> (categoryDict:[String:Any], level1CateArray:[String]) { | |
var categoriesDict:[String:Any] = [:] | |
var level1Categories:[String] = [] | |
if let path = Bundle.main.path(forResource: "Category", ofType: "plist") { | |
let rawData = try! Data(contentsOf: URL(fileURLWithPath: path)) | |
if let categoryDict = try! PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String:Any] { | |
categoriesDict = categoryDict | |
for k in categoryDict.keys { | |
level1Categories.append(k) |
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
let cellTitle:NSString = "string to calculate" | |
var autoSize = cellTitle.size(withAttributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 17.0)]) |
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
// The Pan Gesture | |
func createPanGestureRecognizer(targetView: UIImageView) { | |
var panGesture = UIPanGestureRecognizer(target: self, action:#selector(handlePanGesture(_:))) | |
targetView.addGestureRecognizer(panGesture) | |
} | |
@objc func handlePanGesture(panGesture: UIPanGestureRecognizer) { | |
// get translation | |
let translation = panGesture.translation(in: view) | |
// This line is reset translation point to zero, let the next translation base on |
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
let briefInfoFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ClothItem") | |
briefInfoFetchRequest.resultType = .dictionaryResultType | |
briefInfoFetchRequest.returnsObjectsAsFaults = false | |
let desc = NSExpressionDescription() | |
desc.name = "mainPhoto" | |
desc.expression = NSExpression(forKeyPath: "mainPhotoURLString") | |
desc.isStoredInExternalRecord = true | |
desc.expressionResultType = .stringAttributeType |
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
let itemIDsFetchRequest = NSFetchRequest<NSManagedObjectID>(entityName: "ClothItem") | |
itemIDsFetchRequest.resultType = .managedObjectIDResultType | |
do { | |
let ids = try managedContext!.fetch(itemIDsFetchRequest) | |
print(ids.first!.uriRepresentation().absoluteString) | |
}catch let error as NSError { | |
print("Fetch item failed:\(error), \(error.userInfo)") | |
} |
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
//You can drag a tapGestureRecongizer to the very bottom view, and link a IBAction like below to it | |
@IBAction func closeAllKeyborad(_ sender: Any) { | |
view.endEditing(true) | |
} |
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
//custom cell class | |
import UIKit | |
class CollectionViewCell: UICollectionViewCell { | |
@IBOutlet weak var nameLabel: UILabel! | |
override var isSelected: Bool{ | |
didSet(newValue){ | |
contentView.backgroundColor = newValue ? UIColor.green : UIColor.white |
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
func setupTabBarItem(){ | |
var defaultIconImage = UIImage(named: "035") | |
if let image = defaultIconImage { | |
defaultIconImage = image.withRenderingMode(.alwaysOriginal) //Keep image render model original is important! | |
} | |
var selectedIconImage = UIImage(named: "020") | |
if let image = selectedIconImage { | |
selectedIconImage = image.withRenderingMode(.alwaysOriginal) | |
} |
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
@IBAction func addName(_ sender: Any) { | |
let alert = UIAlertController(title: "New Name", message: "Add a new name", preferredStyle: .alert) | |
let saveAction = UIAlertAction(title: "Save", style: .default) { | |
[unowned self] action in | |
guard let textField = alert.textFields?.first, | |
let nameToSave = textField.text else { | |
return | |
} | |
self.names.append(nameToSave) | |
self.tableView.reloadData() |
NewerOlder