Search

stitchzaa

Some useful code & information for web dev (HTML5, CSS3, JavaScript) and iOS app dev (Swift)

Category

Swift

Import image from photo library into app in swift

Add  UIImagePickerControllerDelegate, UINavigationControllerDelegate  at top of the class.

 

Before viewDidLoad()

var picker = UIImagePickerController()

 

In viewDidLoad() or any function that you want to use image picker

picker.delegate = self

 

Add some function for catch event

// What to do when the picker returns with a photo
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
    dismissViewControllerAnimated(true, completion: nil)
    addAvatar(chosenImage)
//Use image that user pick from library
}

// What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: {() in
                // some code for do anything when user cancel to import image
    })
}

UIScrollView in Swift

Code for create UIScrollView (which named scrollView) and specify size of content (width and height) inside scrollView.

var scrollView = UIScrollView(frame: containerView.frame)

scrollView.contentSize = CGSize(width: 3000, height: 2000)

containerView.addSubview(scrollView)

// can be any view that is container of scrollView


Continue reading “UIScrollView in Swift”

How to integrate MZFormSheetPresentationController into swift project

MZFormSheetPresentationController is an ios framework that provide a popup from UIViewController.

example of MZFormSheetPresentationController
example of MZFormSheetPresentationController

Step to integrate MZFormSheetPresentationController

1. add these 2 lines to podfile and compile
pod 'MZAppearance'
pod 'JGMethodSwizzler'

2. download .zip file from MZFormSheetPresentationController and extract
Screen Shot 2015-08-02 at 5.41.08 AM

3. drag “MZFormSheetPresentationController” folder to your project
Screen Shot 2015-08-02 at 5.41.41 AM

4. in your Bridging Header file, add these 2 lines
#import "MZFormSheetPresentationController.h"
#import "MZFormSheetPresentationControllerSegue.h"

5. create your new UIViewController .xib file, name it (Ex: PopupViewController.xib) and create your own popup layout.
Screen Shot 2015-08-02 at 6.07.59 AM

6. Add these code in the UIViewController that you want to add your popup

class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var btn:UIButton = UIButton.buttonWithType(UIButtonType.InfoLight) as! UIButton
btn.addTarget(self, action: "btnInfoTouched", forControlEvents: UIControlEvents.TouchUpInside)
UIBarButtonItem(customView: btn)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: btn)
}
func btnInfoTouched(){
var infoViewController = UIViewController(nibName: "PopupViewController", bundle: nil)
var formSheetController = MZFormSheetPresentationController(contentViewController: infoViewController)
formSheetController.contentViewSize = CGSizeMake(280, 70)
formSheetController.contentViewControllerTransitionStyle = MZFormSheetPresentationTransitionStyle.StyleBounce
var tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissPopup")
formSheetController.view.addGestureRecognizer(tap)
// add tap gesture recognizer to detect tap and close popup
self.presentViewController(formSheetController, animated: true, completion: nil)
}
func dismissPopup() {
self.dismissViewControllerAnimated(true, completion: nil)
}
}

7. build and see the result

explode and join array in Swift

how to join array to string

var arrayList:[String]!
…..
var newString:String = “-“.join(arrayList)

 &

how to explode string to array

var stringSentence:String!
…..
var
 newArray:[String] = stringSentence.componentsSeparatedByString(“-“)

Create a free website or blog at WordPress.com.

Up ↑