Skip to content

Instantly share code, notes, and snippets.

@rjjohnpatrickcruz
Created February 23, 2015 00:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjjohnpatrickcruz/838e4d94d2f4c752278e to your computer and use it in GitHub Desktop.
Save rjjohnpatrickcruz/838e4d94d2f4c752278e to your computer and use it in GitHub Desktop.
Swift Cheat Sheet (Snippet Code)
Swift Cheatsheet (Snippet Code)
===================
### UIAlertView
```
var title = "Error"
var message = "Your Error Message!"
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
```
### Perform Segue
```
self.performSegueWithIdentifier("dashboard", sender: self)
```
### Insert icon in the UITextField
```
var imageView = UIImageView();
var image = UIImage(named: "user.png");
imageView.image = image;
username.leftView = imageView;
username.leftViewMode = UITextFieldViewMode.Always
imageView.frame = CGRect(x: 100, y: 100, width: 30, height: 20)
view.addSubview(imageView)
```
### Change Placeholder Color
```
txtField.attributedPlaceholder = NSAttributedString(string:" Username",
attributes:[NSForegroundColorAttributeName: UIColor.blueColor()])
```
### Change UIButton border color
```
btn1.layer.borderColor = UIColor.blueColor().CGColor
btn1.layer.borderWidth = 1.0
```
### Change UIButton Font
```
btn1.titleLabel?.font = UIFont.boldSystemFontOfSize(15.0)
```
### Change UITextField border color
```
txt1.layer.borderColor = UIColor.blueColor().CGColor
txt1.layer.borderWidth = 1.0
```
### Dismiss keyboard with swipe gestures
```
var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "dismissKeyboard")
swipe.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipe)
func dismissKeyboard() {
self.txt1.resignFirstResponder()
}
```
### Add delay before executing your code
```
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(utilities.randomProcessTime() * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
// enter your code here ...
}
```
### Hide UINavigation Controller
```
navigationController?.navigationBarHidden = false
```
### Disable auto swipe
```
self.navigationController?.interactivePopGestureRecognizer.enabled = false
```
### Pop ViewController
```
self.navigationController?.popViewControllerAnimated(true)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment