Thursday, 14 September 2017

Give border to UIView in swift3

extension UIView {
    func addTopBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: width)
        self.layer.addSublayer(border)
    }

    func addRightBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
        self.layer.addSublayer(border)
    }

    func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
        self.layer.addSublayer(border)
    }

    func addLeftBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
        self.layer.addSublayer(border)
    }
}

Wednesday, 6 September 2017

Sum of elements in Swift array



let arr = [1,2,3,4,5,6,7,8,9,10]
var sumedArr = arr.reduce(0, {$0 + $1})
print(sumedArr)
The result will be: 55

Format float value with 2 decimal places

let twoDecimalPlaces = String(format: "%.2f", 10.426123)

Tuesday, 29 August 2017

Thursday, 3 August 2017

Longpress event in swift 3



override func viewDidLoad() {
        super.viewDidLoad() 
var longPressGesture : UILongPressGestureRecognizer!
 longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(MainView.handleLongGesture))
        self.view?.addGestureRecognizer(longPressGesture)

  }


    func handleLongGesture() {
        
        print("long press")
       // do Actions 
        }
    }

Wednesday, 2 August 2017

Solve Gradient covers the background view in swift 3



Apply Gradient to View

class Testing: UIViewController {
    
   
    override func viewDidLoad() {
        super.viewDidLoad()
       setGradientBackground()
    }
    
    func setGradientBackground() {
        let colorTop =  UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
        let colorBottom = UIColor(red: 255.0/255.0, green: 94.0/255.0, blue: 58.0/255.0, alpha: 1.0).cgColor
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [ colorTop, colorBottom]
        gradientLayer.locations = [ 0.0, 1.0]
        gradientLayer.frame = self.view.bounds
        
        self.view.layer.addSublayer(gradientLayer) // --> This will hides the background view

    }



-->Some times it will apply full view,  better to apply gradient in viewDidLoad()

replace self.view.layer.addSublayer(gradientLayer) with 
self.view.layer.insertSublayer(gradientLayer, at: 0), this will put the layer "below" all others 

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


background gradient to fill Landscape mode

-> some times gradient view will not cover total view when rotate screen .


we can solve this one to add below code


override func willAnimateRotation(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
        self.view.layer.sublayers?.first?.frame = self.view.bounds
}


        
   

Remove bottom line in navigation bar

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