Thursday 27 April 2017

screen shot

func takeScreenshot(view: UIView) -> UIImageView {
        UIGraphicsBeginImageContext(view.frame.size)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
        return UIImageView(image: image)
    }

Maps


import UIKit
import CoreLocation
import MapKit

class MapViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {

    var locationManager: CLLocationManager?
    var userLocation : MKUserLocation?
    @IBOutlet weak var map: MKMapView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        
        locationManager?.desiredAccuracy = kCLLocationAccuracyBest
        locationManager?.requestWhenInUseAuthorization()
        locationManager?.startUpdatingLocation()
        
        map.showsUserLocation = true
        
        map.delegate = self
        locationManager?.delegate = self

        // Do any additional setup after loading the view.
    }

   


    @IBAction func mapview(_ sender: Any) {
         //map.mapType = MKMapType.hybrid   // satellite view 
        takeScreenshot(view: map) . // screen shot
    }
    
    override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
        if motion == UIEventSubtype.motionShake {
            addAnnotation()
        }
    }
    
    func takeScreenshot(view: UIView) -> UIImageView {
        UIGraphicsBeginImageContext(view.frame.size)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
        
        return UIImageView(image: image)
    }

    
    func addAnnotation() {
        if let uLocation = userLocation {
            let annotation = PizzaAnnotation(coordinate: uLocation.coordinate, title: "Pizaa", subtitle: "Cheese")
            self.map.addAnnotation(annotation)
        }
    }
    
    
    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        let region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 600, 500)
        self.map.setRegion(region, animated: true)
        self.userLocation = userLocation
    }


}




Note: Must on Maps: Project -- > capabilities --> Maps





Camera App Swift 3 iOS 10

import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    
    
    @IBOutlet weak var pickedImaged: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func camerabuttonaction(_ sender: Any) {
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
        {
            let imagepicker = UIImagePickerController()
            imagepicker.delegate = self
            imagepicker.sourceType = UIImagePickerControllerSourceType.camera;
            imagepicker.allowsEditing = false
            self.present(imagepicker,animated: true,completion: nil)
            
            
        }
    }
    
    

    @IBAction func photolibraryaction(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary)
        {
            let imagepicker = UIImagePickerController()
            imagepicker.delegate=self
            imagepicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            imagepicker.allowsEditing = true
            self.present(imagepicker,animated: true,completion: nil)
        }
    }
    
    
    @IBAction func Saveaction(_ sender: Any) {
        
        let imageData = UIImageJPEGRepresentation(pickedImaged.image!, 0.6)
        let compressedJPEGImage = UIImage(data: imageData!)
        UIImageWriteToSavedPhotosAlbum(compressedJPEGImage!, nil, nil, nil)
        saveNotice()
    }
    
//    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//        pickedImaged.image = image
//        self.dismiss(animated: true, completion: nil)
//        
//        
//        
//    }
    
//    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image:UIImage!, edit[NSObject : AnyObject]!)
//    {
//        self.dismiss(animated: true, completion: nil);
//    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            pickedImaged.contentMode = .scaleAspectFit
            pickedImaged.image = pickedImage
        }
        self.dismiss(animated: true, completion: nil)
    };
    
    func saveNotice()
    {
        let alertController = UIAlertController(title: "Title", message: "succesfully saved",preferredStyle: .alert)
        let defaultAction = UIAlertAction(title:"OK", style: .default, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController,animated: true,completion: nil)
        
    }
    
}


----------------------------------------------------------------------------------------------



Adjust UILabel height or width to text

  • First, you need to add contains height for UILabel
  • Then, modify the Relation from Equal to Greater than or Equal
enter image description here
  • Finally, change the lines of UILabel from 1 to 0
enter image description here
Your UILabel will automatically increase height depending on the text

How to hide keyboard in swift on pressing return key?

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var myTextField : UITextField

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.myTextField.delegate = self;
    }

 func textFieldShouldClear(_ textField: UITextField) -> Bool // called when 'return' key pressed. return false to ignore.
{
        textField.resignFirstResponder()
        return true
    }
}

Wednesday 26 April 2017

tab bar key board issue swift 3

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showLogin"{
let bottomBar = segue.destinationViewController as LoginViewController
println("showLogin seque called")
bottomBar.navigationItem.hidesBackButton = true // hide back button
bottomBar.hidesBottomBarWhenPushed = true // hide bar menu }
}

Tuesday 18 April 2017

How to recognize swipe in all 4 directions

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(swipeRight)

    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeDown.direction = UISwipeGestureRecognizerDirection.down
    self.view.addGestureRecognizer(swipeDown)
}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.up:
            print("Swiped up")
        default:
            break
        }
    }
}

How to hide keyboard in swift on pressing return key or touch any where in View




class ViewController: UIViewController,UITextFieldDelegate{

 @IBOutlet weak var chatting_Textfield: UITextField!
  
    override func viewDidLoad() {
        super.viewDidLoad()
{
self.chatting_Textfield.delegateself
}


   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        chatting_Textfield.resignFirstResponder()
        return (true)
    }

}

Monday 17 April 2017

Framework Library not loaded: 'Image not found'

--> http://stackoverflow.com/questions/24993752/os-x-framework-library-not-loaded-image-not-found/41401354#41401354

-->http://stackoverflow.com/questions/26024100/dyld-library-not-loaded-rpath-libswiftcore-dylib/41401399#41401399

delete derived data in xcode-8

http://stackoverflow.com/questions/38016143/how-to-delete-derived-data-in-xcode-8

Friday 14 April 2017

iOS 10.2 (14C92), iOS 10.3 which may not be supported by this version of Xcode

  1. Copy this iOS 10.2 image file to:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/
  2. Restart Xcode.
Now it Working fine.

Wednesday 12 April 2017

Line Breaks and Number of Lines based on data in Swift Label (Programmatically)




Change the values in story board of label values to 

Lines --> 0

Line Break -- > Word wrap





override func viewDidLoad() {        super.viewDidLoad(){self.chatdisplayTableview.estimatedRowHeight = 600.0;        self.chatdisplayTableview.rowHeight = UITableViewAutomaticDimension;        self.chatdisplayTableview.setNeedsLayout()        self.chatdisplayTableview.layoutIfNeeded()}

Swift programmatically navigate to another view controller with navigation bar with back button / without navigation bar



Navigate to another view controller with navigation bar with backbutton

 let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyBoard.instantiateViewController(withIdentifier: "secondviewcontrollerid") as! secondviewcontroller
        
vc.message="value"
       
self.navigationController?.pushViewController(vc, animated: true)


-----------------------------------------------------------------------------------------------------------------------

        


Navigate to another view controller without navigation bar

let vc=storyBoard.instantiateViewController(withIdentifier: "secondviewcontrollerid"assecondviewcontroller
        
vc.message="value"
       
self.present(vc, animated: true, completion: nil)

Tuesday 11 April 2017

Apply material desings icons



step - 1
https://github.com/google/material-design-icons/blob/master/iconfont/MaterialIcons-Regular.ttf


step - 2
update info file

       


Step-3

Update Build phase

   


step - 4

Apply it into story board



Icons names with values


  • https://gist.github.com/monyschuk/9769838556874e01eb7686bc675b1e12
  • https://material.io/icons/

step -5 

Apply it into code





Thursday 6 April 2017

display cells only it contains data in tableview or eliminate extra empty cells in UI Tableview in swift 3

override func viewDidLoad() {
        super.viewDidLoad()

        self.yourtableview.tableFooterView = UIView()
}

Wednesday 5 April 2017

Wait until swift for loop with asynchronous(Alamofire) network requests finishes executing

http://stackoverflow.com/questions/35906568/wait-until-swift-for-loop-with-asynchronous-network-requests-finishes-executing

Array is still empty even after being set in DispatchQueue.main.async Swift 3

http://stackoverflow.com/questions/40199012/array-is-still-empty-even-after-being-set-in-dispatchqueue-main-async-swift-3

http://stackoverflow.com/questions/37805885/how-to-create-dispatch-queue-in-swift-3

Remove bottom line in navigation bar

navigationController ?. navigationBar . setBackgroundImage ( UIImage (), for: . any , barMetrics: . default )          navigat...