Padding in UITextfield in both left and right side iOS swift

 Problem:

I want to add padding in left and right side of uitextfield.

How to achieve that?

Solution:

extension UITextField {

    func setPadding() {

        self.setLeftPaddingPoints()

        self.setRightPaddingPoints()

    }

    

    func setLeftPaddingPoints(_ amount:CGFloat = 5){

        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 = 5) {

        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))

        self.rightView = paddingView

        self.rightViewMode = .always

    }

}


Then call the functions as like below.


        myTF.setPadding()

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift