【问题标题】:Customise UITextField in SWIFT [duplicate]在 SWIFT 中自定义 UITextField [重复]
【发布时间】:2017-12-24 12:04:33
【问题描述】:

如何在swift 3 IOS中使用UITextField实现下屏。

【问题讨论】:

标签: ios swift swift3


【解决方案1】:

这样做...

  • 左侧图标图片添加UIImageView,设置所需图片和背景颜色
  • 在图像视图旁边添加UITextField,并在界面构建器(故事板)中将边框样式设置为“无边框样式”
  • 设置所需的背景颜色
  • 也对密码字段执行相同操作,对于眼睛图标视图,请查看https://github.com/Sahilberi/ImageTextField

【讨论】:

    【解决方案2】:
    1. 添加UITextField

    2. 将子视图添加为 imageView

    3. 设置 UITextField

    4. 的 Alpha

    下面的示例代码:

    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)
    }
    

    【讨论】:

      【解决方案3】:

      导入 UIKit

      类视图控制器: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
      }
      

      }

      //看看这个输出

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        • 2015-07-18
        • 2015-08-13
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-01
        相关资源
        最近更新 更多