You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS集成Material Design时MDCTextInputControllerUnderline未检测到问题求助

Fixing MDCTextInputControllerUnderline Not Detected in iOS Material Components

Hey there! I’ve run into this exact snag before when working with Material Components for iOS, so let’s walk through the most common fixes to get MDCTextInputControllerUnderline working for you.

1. Double-Check Your Dependencies & Imports

First, make sure you’ve got the right components set up correctly:

  • If you’re using CocoaPods, confirm your Podfile includes the TextFields module:
    pod 'MaterialComponents/TextFields'
    
    Run pod install or pod update to pull in the latest compatible version.
  • In your Swift file, import the explicit module to ensure the controller is recognized:
    import MaterialComponents.MaterialTextFields
    
    (For Objective-C projects, add #import "MaterialComponents/MaterialTextFields.h" to your bridging header.)

Your current code creates an MDCTextField, but you haven’t connected it to an MDCTextInputControllerUnderline instance—this is mandatory to enable the underline styling. Here’s how to fix your code:

class ViewController: UIViewController {
    let userNameTextField: MDCTextField = {
        let textField = MDCTextField()
        textField.placeholder = "Full Name"
        // Disable autoresizing mask so Auto Layout works
        textField.translatesAutoresizingMaskIntoConstraints = false
        return textField
    }()
    
    // Declare the controller instance to hold the underline configuration
    private var userNameInputController: MDCTextInputControllerUnderline!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(userNameTextField)
        
        // Initialize the underline controller and link it to your text field
        userNameInputController = MDCTextInputControllerUnderline(textInput: userNameTextField)
        
        // Fix your Auto Layout constraints (note the corrected "anchor" spelling)
        NSLayoutConstraint.activate([
            userNameTextField.topAnchor.constraint(equalTo: signUpButton.bottomAnchor, constant: 20),
            userNameTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16),
            userNameTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -16),
            userNameTextField.heightAnchor.constraint(equalToConstant: 48)
        ])
    }
}

3. Fix Common Code Typos & Oversights

I spotted a couple of small issues in your original snippet that might cause unexpected errors:

  • You named your text field userNameTextField but tried to add m_userNameTextField to the view—make sure variable names match consistently.
  • acnchor is a typo; it should be anchor.
  • Always set translatesAutoresizingMaskIntoConstraints = false when using programmatic Auto Layout to avoid conflicts.

4. Verify Version Compatibility

If you’re still stuck, ensure your Material Components version aligns with your target iOS version. The latest releases support iOS 13+, so if you’re targeting an older OS, specify a compatible version in your Podfile (e.g., pod 'MaterialComponents/TextFields', '~> 120.0').

内容的提问来源于stack exchange,提问作者M Nur Firdaus Izrullah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:47:39