Saturday, November 19, 2016

Swift 3 Adding sublayers to views

Adding sub layers to UIControls helps us a lot in making UI better and simple.

Let’s see the below screen and think of achieving it.



Adding thin underline sub layer to text fields give the required design. 


  
        let userNameFrame = self.tfUserName.frame
        let userNameLayer = CALayer.init()
        userNameLayer.frame = CGRect.init(x: userNameFrame.origin.x,
                                          y: userNameFrame.origin.y + userNameFrame.size.height,
                                          width: userNameFrame.size.width,
                                          height: 1)
        userNameLayer.backgroundColor = UIColor.lightGray.cgColor
        self.view.layer.addSublayer(userNameLayer)

        



        let passwordFrame = self.tfPassword.frame
        let passwordLayer = CALayer.init()
        passwordLayer.frame = CGRect.init(x: passwordFrame.origin.x,
                                          y: passwordFrame.origin.y + passwordFrame.size.height,
                                          width: passwordFrame.size.width,
                                          height: 1)
        passwordLayer.backgroundColor = UIColor.lightGray.cgColor
        self.view.layer.addSublayer(passwordLayer)

        
Simple and short.


Hope this post is useful. Feel free to comment incase of any queries.