Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create a textField with left Side image and bottom border line in swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.7k
    Comment on it

    1- Create a new class subclass of UITextField.

    2 -Write down code below in new created class

    3- Add a textField in ios Your viewController.

    4- Chnage th class of your TextField with the new class that you created.

    5- Change the attribute of textField properties from Attribute instepector

     


     

    import UIKit
    
    @IBDesignable
    
    class UnderLineImageTextField: UITextField {
        override func drawRect(rect: CGRect) {
            
            let borderLayer = CALayer()
            let widthOfBorder = getBorderWidht()
            borderLayer.frame = CGRectMake(-15, self.frame.size.height - widthOfBorder, self.frame.size.width+20, self.frame.size.height)
            borderLayer.borderWidth = widthOfBorder
            borderLayer.borderColor = getBottomLineColor()
            self.layer.addSublayer(borderLayer)
            self.layer.masksToBounds = true
            
        }
        
        
        
        //MARK : set the image LeftSide
        @IBInspectable var SideImage:UIImage? {
            didSet{
                
                let leftAddView = UIView.init(frame: CGRectMake(0, 0, 25, self.frame.size.height-10))
                let leftimageView = UIImageView.init(frame: CGRectMake(0, 0, 20, 20))//Create a imageView for left side.
                leftimageView.image = SideImage
                leftAddView.addSubview(leftimageView)
                self.leftView = leftAddView
                self.leftViewMode = UITextFieldViewMode.Always
            }
            
        }
        @IBInspectable var bottomLineColor: UIColor = UIColor.blackColor() {
            didSet {
                
            }
        }
        
        
        func getBottomLineColor() -> CGColor {
            return bottomLineColor.CGColor;
            
        }
        @IBInspectable var cusborderWidth:CGFloat = 1.0
            {
            didSet{
                
            }
        }
        
        func getBorderWidht() -> CGFloat {
            return cusborderWidth;
            
        }
    }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: