Posts

Showing posts from 2015

Work With CollectionViewCell In TableView In Swift Ios

override func viewDidLoad () { super . viewDidLoad () tableView = UITableView ( frame : self . view . bounds ) tableView . delegate = self tableView . dataSource = self self . view . addSubview ( tableView ) tableView . registerClass ( TableViewCell . self ,   forCellReuseIdentifier : "TableViewCell" ) tableView . registerClass ( UITableViewCell . self ,   forCellReuseIdentifier : "NormalCell" ) } func numberOfSectionsInTableView ( tableView : UITableView ) -> Int { return 1 } func tableView ( tableView : UITableView , numberOfRowsInSection section : Int ) ->   Int { return 5 } func tableView ( tableView : UITableView ,   cellForRowAtIndexPath indexPath : NSIndexPath ) -> UITableViewCell { if indexPath . row == 3 { var cell : TableViewCell = tableView . dequeueReusableCellWithIdentifier   ( "TableViewCell" , forIndexPath : indexPath ) as !

How To Add Multiple Line in Lable in Swift Ios

textLabel . lineBreakMode = . ByWordWrapping // or NSLineBreakMode.ByWordWrapping textLabel . numberOfLines = 0 // Objective-C textLabel . lineBreakMode = NSLineBreakByWordWrapping ; textLabel . numberOfLines = 0 ; // C# (Xamarin.iOS) textLabel . LineBreakMode = UILineBreakMode . WordWrap ; textLabel . Lines = 0 ;

Work With KVO In Swift Ios.Iphone

class CarObserver : NSObject { private var kvoContext : UInt8 = 1 private let car : Car init ( _ car : Car ) { self . car = car super . init () car . addObserver ( self , forKeyPath : "miles" , options : NSKeyValueObservingOptions . New , context : & kvoContext ) } override func observeValueForKeyPath ( keyPath : String , ofObject object : AnyObject , change : [ NSObject : AnyObject ], context : UnsafeMutablePointer < Void >) { if context == & kvoContext { println ( "Change at keyPath = \(keyPath) for \(object)" ) } } deinit { car . removeObserver ( self , forKeyPath : "miles" ) } }

How To USe By Default Selecected Row And Checkmark UITableView In Swift

let indexPath = NSIndexPath ( forRow : 0 , inSection : 0 ); self . tableView . selectRowAtIndexPath ( indexPath , animated : false ,    scrollPosition : UITableViewScrollPosition . None ) self . tableView ( self . tableView , didSelectRowAtIndexPath : indexPath )   func tableView ( tableView : UITableView !, didSelectRowAtIndexPath  indexPath : NSIndexPath !) { NSLog ( "You selected cell number: \(indexPath.row)!" ) self . performSegueWithIdentifier ( "yourIdentifier" , sender : self )  }       override func tableView ( tableView : UITableView , didSelectRowAtIndexPath  indexPath : NSIndexPath ) { let indexPath = tableView . indexPathForSelectedRow () let currentCell = tableView . cellForRowAtIndexPath ( indexPath )     as UITableViewCell println ( currentCell . textLabel !. text )           override func viewDidLoad () { super . viewDidLoad () // … tableView . register

How To Hide Navigation Bar button in Swift

[[ self navigationController ] setNavigationBarHidden : YES animated : YES ];   [[ self navigationController ] setNavigationBarHidden : NO animated : YES ];   override func viewWillAppear ( animated : Bool ) { self . navigationController ?. navigationBarHidden = true }       override func viewWillAppear ( animated : Bool ) { self . navigationController ?. navigationBarHidden = false }     self . navigationItem . setHidesBackButton ( true , animated : false )

Work With NSLocal Country code in Swift

var pre = NSLocale . preferredLanguages ()[ 0 ]   var preferredLanguages : NSLocale ! let pre = preferredLanguages . displayNameForKey ( NSLocaleIdentifier ,   value : preferredLanguages )

Work With NSNotification Center In Swift Ios

  NSNotificationCenter . defaultCenter (). addObserver ( self , selector : "batteryLevelChanged:" , name : UIDeviceBatteryLevelDidChangeNotification , object : nil )     @objc func batteryLevelChanged ( notification : NSNotification ){ //do stuff }   ========================================= Send(Post) NSNotificationCenter . defaultCenter (). postNotificationName   ( "NotificationIdentifier" , object : nil )   Receive(Get) Notification   NSNotificationCenter . defaultCenter (). addObserver ( self , selector :     "methodOfReceivedNotification:" , name : "NotificationIdentifier" , object : nil )   Remove Notification NSNotificationCenter . defaultCenter (). removeObserver   ( self , name : "NotificationIdentifier" , object : nil ) NSNotificationCenter . defaultCenter (). removeObserver   ( self ) // Remove from all notifications being observed     Method h

How To Use Perfom Selector In Swift Ios/Iphone

NSTimer . scheduledTimerWithTimeInterval ( delay , target : self ,   selector : "onFlip" , userInfo : nil , repeats : false )     func performFlipOnDelay () { let delayInSeconds = 1.0 let delay = dispatch_time ( DISPATCH_TIME_NOW , Int64   ( delayInSeconds * Double ( NSEC_PER_SEC ))) dispatch_after ( delay , dispatch_get_main_queue ()) { onFlip (); } }

How To Chanage Navigation bar Color in Swift Ios

=> This code changes the NavigationBar color navigationController . navigationBar . barTintColor = UIColor . greenColor ()   => Navigation Bar Text:   navigationController . navigationBar . titleTextAttributes =     [ UITextAttributeTextColor : UIColor . orangeColor ()]      => Tab Bar Color:--   tabBarController . tabBar . tintColor = UIColor . yellowColor ()     => Change Bar Button Item:- UIBarButtonItem . appearance (). setTitleTextAttributes   ([ NSForegroundColorAttributeName : color , NSFontAttributeName : buttonFont ],   forState : UIControlState . Normal )

Pass Index One To Another Controller Swift Ios

var valueToPass : String ! func tableView ( tableView : UITableView !, didSelectRowAtIndexPath indexPath : NSIndexPath !) { println ( "You selected cell #\(indexPath.row)!" ) // Get Cell Label let indexPath = tableView . indexPathForSelectedRow (); let currentCell = tableView . cellForRowAtIndexPath ( indexPath !) as UITableViewCell !; valueToPass = currentCell . textLabel . text performSegueWithIdentifier ( "yourSegueIdentifer" , sender : self ) } override func prepareForSegue ( segue : UIStoryboardSegue , sender : AnyObject ?) { if ( segue . identifier == "yourSegueIdentifer" ) { // initialize new view controller and cast it as your view controller var viewController = segue . destinationViewController as AnotherViewController // your new view controller should have property that will store passed value viewController . passedValue = valueToPass

How To Know UIButton Pressed in Swift Ios

var button = UIButton ( frame : CGRect ( x : 0 , y : 0 , width : 150 , height : 60 )) button . backgroundColor = UIColor . blackColor () button . layer . cornerRadius = 3.0 button . setTitle ( "Tap Me" , forState : . Normal ) button . addTarget ( self , action : "buttonTapped" , forControlEvents : . TouchUpInside )   func buttonTapped () { println ( "Button tapped!" ) }

How to Use pagination ScrollView in Swift Ios/Iphone

agedScrollViewController.swift import UIKit class PagedScrollViewController: UIViewController, UIScrollViewDelegate {   @IBOutlet var scrollView: UIScrollView!   @IBOutlet var pageControl: UIPageControl! } var pageImages: [UIImage] = [] var pageViews: [UIImageView?] =[] override func viewDidLoad() {   super.viewDidLoad()   // 1   pageImages = [UIImage(named: "photo1.png")!,     UIImage(named: "photo2.png")!,     UIImage(named: "photo3.png")!,     UIImage(named: "photo4.png")!,     UIImage(named: "photo5.png")!]   let pageCount = pageImages.count   // 2   pageControl.currentPage = 0   pageControl.numberOfPages = pageCount   // 3   for _ in 0..<pageCount {     pageViews.append(nil)   }   // 4   let pagesScrollViewSize = scrollView.frame.size   scrollView.contentSize = CGSize(width: pagesScrollViewSize.width * CGFloat (pageImages.count),                                   height: pagesScrollViewSize.height)   // 5   loadVisiblePages()

How to Use Multiple Cell In Swift UITableView IOs

unc tableView ( tableView : UITableView , cellForRowAtIndexPath indexPath : NSIndexPath ) -> UITableViewCell { cell . userInteractionEnabled = false if indexPath . section == 0 { let cell : FlightsDetailCell = tableView . dequeueReusableCellWithIdentifier ( "Cell" , forIndexPath : indexPath ) as FlightsDetailCell cell . graphView . enableBezierCurve = true cell . graphView . enableReferenceYAxisLines = true cell . graphView . enableYAxisLabel = true cell . graphView . colorYaxisLabel = UIColor . whiteColor () cell . graphView . delegate = UIApplication . sharedApplication (). delegate as   BEMSimpleLineGraphDelegate cell . graphView . dataSource = UIApplication . sharedApplication (). delegate as BEMSimpleLineGraphDataSource return cell } else if indexPath . section == 1 { let cell : FlightsDetailCell = tableView . dequeueReusableCellWithIdentifier ( "Cell" , forIndexP

How To Use UIScrollView In Swift

o to the ViewController.swift file and add the follwing properties var imageView: UIImageView! var scrollView: UIScrollView! override func viewDidLoad() {       super.viewDidLoad()   // 1     imageView = UIImageView(image: UIImage(named: "strandvagen.jpg"))      // 2   scrollView = UIScrollView(frame: view.bounds)   scrollView.backgroundColor = UIColor.blackColor()   // 3   scrollView.contentSize = imageView.bounds.size   // 4        scrollView.addSubview(imageView)   view.addSubview(scrollView) } class ViewController: UIViewController, UIScrollViewDelegate { @IBOutlet var scrollView: UIScrollView! var imageView: UIImageView! override func viewDidLoad() {   super.viewDidLoad()   // 1   let image = UIImage(named: "photo1.png")!   imageView = UIImageView(image: image)   imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:image.size)   scrollView.addSubview(imageView)   // 2   scrollView.contentSize = image.size   // 3   var doubleTapRecognizer = UITapGestureRe

Set Scrollview in Swift

override func viewWillLayoutSubviews () { super . viewWillLayoutSubviews (); self . scrollView . contentSize . height = 3000 ; // Or whatever you want it to be. }   override func viewWillLayoutSubviews () { super . viewWillLayoutSubviews (); self . scrollView . frame = self . view . bounds ; // Instead of using auto layout self . scrollView . contentSize . height = 3000 ; // Or whatever you want it to be. }

Draw rout using mapkit Swift

mapView ( mapView : rendererForOverlay ) rendererForOverlay ( overlay :)     func mapView ( mapView : MKMapView !, rendererForOverlay overlay :   MKOverlay !) -> MKOverlayRenderer ! { println ( "rendererForOverlay" ); if ( overlay is MKPolyline ) { var pr = MKPolylineRenderer ( overlay : overlay ); pr . strokeColor = UIColor . blueColor (). colorWithAlphaComponent ( 0.5 ); pr . lineWidth = 5 ; return pr ; } return nil }

How TO Work MKAnotation In Swift Ios

func mapView ( mapView : MKMapView !, annotationView view :     MKAnnotationView !, didChangeDragState newState :     MKAnnotationViewDragState , fromOldState oldState :     MKAnnotationViewDragState ) {   switch ( newState ) {   case . Starting : view . dragState = . Dragging case . Ending , . Canceling : view . dragState = . None default : break   } }

USe Of Simple TableView:In Swift Ios

var cell : UITableViewCell? var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell if cell == nil {     cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL") } //we know that cell is not empty now so we use ! to force unwrapping cell!.textLabel.text = "Baking Soda" cell!.detailTextLabel.text = "1/2 cup" cell!.textLabel.text = "Hello World" return cell Here's an example using a custom cell class: override func viewDidLoad() {     super.viewDidLoad()     self.tableView.registerClass(MyCell.self, forCellReuseIdentifier: "Cell") } override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as MyCell     // no "if" - the cell is guaranteed to exist     // ... do stuff to the cell here ...     cell.tex

Custom UITableViewCell in Swift Ios

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {     var  cell:SimpleTableCell! = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as? SimpleTableCell     if (cell == nil)     {         let nib:Array = NSBundle.mainBundle().loadNibNamed("SimpleTableCell", owner: self, options: nil)         cell = nib[0] as? SimpleTableCell     }     cell.nameLabel.text = tableData[indexPath.row]     cell.thumbnailImageView.image = UIImage(named:thumbnails[indexPath.row])     cell.prepTimeLabel.text = prepTime[indexPath.row];     return cell; }

How to Zoom UIScrollView In Swift

 ScrollViewController.swift var scrollView: UIScrollView! var imageView: UIImageView! override func viewDidLoad() {     super.viewDidLoad()             imageView = UIImageView(image: UIImage(named: "image.png"))             scrollView = UIScrollView(frame: view.bounds)     scrollView.backgroundColor = UIColor.blackColor()     scrollView.contentSize = imageView.bounds.size     scrollView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight             scrollView.addSubview(imageView)     view.addSubview(scrollView) } scrollView.contentOffset = CGPoint(x: 1000, y: 450) ============================================================== class ScrollViewController: UIViewController, UIScrollViewDelegate { func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {     return imageView }     viewDidLoad(){     scrollView.delegate = self        scrollView.minimumZoomScale = 0.1     scrollView.maximumZoomScale = 4.0     scrollView.zoomSca