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...