Monday 29 May 2017

Update constraints of other elements when UITableView height change? or Dynamic Height Issue for UITableView Cells (Swift)

https://stackoverflow.com/questions/34759302/update-constraints-of-other-elements-when-uitableview-height-change



 @IBOutlet weak var tableheight: NSLayoutConstraint!

 @IBOutlet weak var displayInvoicetableview: UITableView!

    

 override func viewDidLayoutSubviews()
    {
        tableheight.constant = self.displayInvoicetableview.contentSize.height
        self.view.layoutIfNeeded()
        

           }


-------------------------------------
after reload the table view it may not works then the solution for this in below link

https://stackoverflow.com/questions/32886811/when-is-it-most-appropriate-to-update-uitableview-height-nslayoutconstraint

App installation failed: Unknown Error Xcode 8

I have had luck by disconnecting my iPhone from my mac, and then Analyzing (Shift + Command + B or Product -> Analyze).
After that I plugged my iPhone back in and did a clean (Command + k) and then ran! Worked!

Monday 15 May 2017

display Loading using NVActivityIndicatorView

step - 1:

https://github.com/ninjaprox/NVActivityIndicatorView#animation-types
read the document 

Step -2

install using pod file
pod 'NVActivityIndicatorView'

Step-3
import NVActivityIndicatorView

step -4


class Classname: UIViewController, UIScrollViewDelegate,NVActivityIndicatorViewable

step-5
  • Start animating loading


        let activityData = ActivityData()
        NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
        NVActivityIndicatorPresenter.sharedInstance.setMessage("Loading")


  •     stop animating loading



        NVActivityIndicatorPresenter.sharedInstance.stopAnimating()

Convert “yyyy-MM-dd'T'HH:mm:ssZ” format string to date object


function: 


 func date(person: String) -> String
    {
       
        
        let dateFormatter = DateFormatter()
        let tempLocale = dateFormatter.locale // save locale temporarily
        dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
        let date = dateFormatter.date(from: person)!

//dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
        dateFormatter.dateFormat = "yyyy/MM/dd"
        dateFormatter.locale = tempLocale // reset the locale
        let dateString = dateFormatter.string(from: date)
       // print("EXACT_DATE : \(dateString)")
        return dateString

    }

----------------------------------------------------------
Usage:

let datestring=date(person: "2017-01-27T18:36:36Z")


Sunday 14 May 2017

How to Toast message in Swift?

extension UIViewController {

func showToast(message : String) {

    let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
    toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
    toastLabel.textColor = UIColor.white
    toastLabel.textAlignment = .center;
    toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
    toastLabel.text = message
    toastLabel.alpha = 1.0
    toastLabel.layer.cornerRadius = 10;
    toastLabel.clipsToBounds  =  true
    self.view.addSubview(toastLabel)
    UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
         toastLabel.alpha = 0.0
    }, completion: {(isCompleted) in
        toastLabel.removeFromSuperview()
    })
} }

Friday 12 May 2017

How do i add a cancel on a navigation bar programatically



let btnCancel = UIButton()
btnCancel.setImage(UIImage(named: "crossbuttonimagename"), forState: .Normal)
btnCancel.frame = CGRectMake(0, 0, 25, 25)
btnCancel.addTarget(self, action: Selector("youraction"), forControlEvents: .TouchUpInside)

//Set Left Bar Button item
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnCancel
self.navigationItem.leftBarButtonItem = leftBarButton

UIView Hide/Show with animation



function: 

 func setView(view: UIView, hidden: Bool) {
     UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: { _ in
          view.isHidden = hidden
     }, completion: nil) 

}

usage:


@IBOutlet weak var updateprofileview: UIView!

 @IBAction func updateProfileViewAction(_ sender: Any) {
        
        if updateprofileview.isHidden {
            setView(view: updateprofileview, hidden: false)
        } else {
            setView(view: updateprofileview, hidden: true)
        }
    }


note: design view in stack view 

Tuesday 9 May 2017

how to enable unit test cases for already existing project in swift 3


First of all just add new Target via File > New > Target... and select iOS Unit Testing Bundle. Done.

RoundCorners(individual border radius) for Label or Text field or button


Function: 

extension UIView {
    func roundCorners(corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }

}

Usage:

override func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false
        Label.roundCorners(corners: [.topLeft, .bottomLeft], radius: 8)
     
        }

Textfield padding


Function:

extension UITextField {
    func setLeftPaddingPoints(_ amount:CGFloat){
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
        self.leftView = paddingView
        self.leftViewMode = .always
    }
    func setRightPaddingPoints(_ amount:CGFloat) {
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
        self.rightView = paddingView
        self.rightViewMode = .always
    }

}

Usage:

override func viewDidLoad() {
        super.viewDidLoad()

  textfield.setLeftPaddingPoints(8)
}

     

Apply Radius to button




registration_button.layer.cornerRadius = 5



Remove bottom line in navigation bar

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