【问题标题】:How to use "MDCOutlinedTextField" in Swift如何在 Swift 中使用“MDCOutlinedTextField”
【发布时间】:2021-12-20 19:00:57
【问题描述】:

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

所以我使用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。

所以我添加了另一个框架 TextControls 在这里我添加了 pod '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

【问题讨论】:

    标签: ios swift textfield


    【解决方案1】:

    按照example 尝试设置label.text 这样的属性

    textField.label.text = "Phone number"
    

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

    【讨论】:

    • 我需要密码文本字段的眼睛按钮如何添加?
    • 我已在问题末尾使用密码文本字段图像编辑了我的问题.. 那样怎么办?
    • @User123 尝试使用您的自定义视图将其分配给trailingView 属性
    猜你喜欢
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2022-11-06
    • 2015-09-21
    • 2021-04-05
    • 2014-09-18
    相关资源
    最近更新 更多