首页 > 解决方案 > 如何在 Swift 中使用“MDCOutlinedTextField”

问题描述

我需要如下图所示的文本字段

在此处输入图像描述

所以我使用material-components-ios框架,为此我在情节提要中添加了文本字段并将其类名MDCTextField和代码

import UIKit

import MaterialComponents
class LoginVC: UIViewController {

@IBOutlet weak var emailTF: MDCTextField!

var textFieldControllerFloating: MDCTextInputControllerOutlined?

override func viewDidLoad() {
    super.viewDidLoad()
  //  setupEditText()
    
    textFieldControllerFloating = MDCTextInputControllerOutlined(textInput: emailTF)

    textFieldControllerFloating?.activeColor = UIColor.lightGray
    textFieldControllerFloating?.floatingPlaceholderActiveColor = UIColor.green
    textFieldControllerFloating?.normalColor = UIColor.lightGray
    textFieldControllerFloating?.inlinePlaceholderColor = UIColor.lightGray
    
}

这段代码 o/p 完美但显示警告

警告:

'MDCTextField' 已弃用:MDCTextField 及其关联的类已弃用。请改用 TextControls。

所以我在这里添加了另一个框架TextControlspod 'MaterialComponents/TextControls+OutlinedTextFields'我在 stroyboard中添加和添加了文本字段,并将其类赋予MDCOutlinedTextField并添加到代码下方

 import UIKit
 import MaterialComponents.MaterialTextControls_OutlinedTextFields

class LoginVC: UIViewController {

@IBOutlet weak var emailTF: MDCOutlinedTextField!
@IBOutlet weak var passwordTF: MDCOutlinedTextField!
}
}

但是o / p如下所示:没有浮动占位符文本和小高度,为什么?,我错在哪里..如何获得高度为50和浮动占位符边框的文本字段,如上图,请帮忙。

编辑:我需要这样的密码文本字段的眼睛按钮怎么办MDCOutlinedTextField

在此处输入图像描述

标签: iosswifttextfield

解决方案


根据示例尝试设置label.text这样的属性

textField.label.text = "Phone number"

至于文本字段高度,您可以使用 Interface Builder 中的约束来设置它


推荐阅读