2017-07-19 88 views
-8

如何使用swift 3 IOS中的UITextField实现下面的屏幕。在SWIFT中自定义UITextField

enter image description here

+0

什么是你的问题? –

+0

@Anil你想定制UITextField或UITextView。 –

+1

检查这个答案 https://stackoverflow.com/questions/27903500/swift-add-icon-image-in-uitextfield – Arun

回答

0

做这样的...

  • 添加UIImageView为左侧图标图像,需要设定图像和背景颜色
  • 添加UITextField的ImageView的旁边,设置边框样式“没有边框样式'
  • 设置所需的背景颜色
  • D Ø相同的密码字段也,而对于眼睛图标视图看看这个https://github.com/Sahilberi/ImageTextField
0
  1. 添加的UITextField

  2. 添加子视图为ImageView的

  3. 的字母UITextField

下面的示例代码为:

func addTextField() { 
     let txtUserName = UITextField(frame: CGRect(x: 30, y: 70, width: 320, height: 45)) 
     let txtPassword = UITextField(frame: CGRect(x: 30, y: 140, width: 320, height: 45)) 
     txtUserName.backgroundColor = UIColor.black 
     txtPassword.backgroundColor = UIColor.black 

     txtUserName.alpha = 0.35 
     txtPassword.alpha = 0.35 

     let imgUser = UIImageView(frame: CGRect(x: 5, y: 5, width: 35, height: 35)) 
     let imgKey = UIImageView(frame: CGRect(x: 5, y: 5, width: 35, height: 35)) 
     let imgEye = UIImageView(frame: CGRect(x: txtPassword.frame.size.width - 40, y: 5, width: 35, height: 35)) 
     imgUser.image = UIImage(named: "user") 
     imgKey.image = UIImage(named: "key") 
     imgEye.image = UIImage(named: "eye") 
     txtUserName.addSubview(imgUser) 
     txtPassword.addSubview(imgKey) 
     txtPassword.addSubview(imgEye) 
     view.addSubview(txtUserName) 
     view.addSubview(txtPassword) 
} 
0

进口的UIKit

类的ViewController:UIViewController的{

@IBOutlet weak var txtpassword: UITextField! 
@IBOutlet weak var txtUserName: UITextField! 
@IBOutlet weak var txtView: UITextView! //your textView 


override func viewDidLoad() { 

    super.viewDidLoad() 
    //navigationBarColor() 
    setTextFieldSpace(textField :txtUserName) 
    setTextFieldSpace(textField :txtpassword) 
    //textField border 
    txtpassword.borderStyle = .none 
    txtUserName.borderStyle = .none 

} 

//MARK: - Button Action 
// password show hide 
@IBAction func btnShowPasswordClick(_ sender: UIButton) { 
    if txtpassword.isSecureTextEntry{ 
     txtpassword.isSecureTextEntry = false 
    }else{ 
     txtpassword.isSecureTextEntry = true 
    } 
} 

//for set space in left side of textField 
func setTextFieldSpace(textField :UITextField){ 
    let lblSpace = UILabel() 
    lblSpace.frame = CGRect.init(x: 0, y: 0, width: 5, height: 5) 
    lblSpace.backgroundColor = .clear 
    textField.leftView = lblSpace 

    textField.leftViewMode = .always 
    textField.contentVerticalAlignment = .center 
} 

}

//检查出了这一点把

your screen