Custom `UITextField` subclass doesn't appear on the screen

237

Solution 1

The most probable reason at the moment is that you set the text field frame to zero. The view is on the screen at (0, 0) and its size is (0, 0) - so you won't see the text field.

To make it easier for testing set the background color to something conspicuous:

dnmTextField.backgroundColor = .red

and create the view with non-zero size frame:

dnmTextField = DnmTextField(frame: CGRect(x: 50, y: 50, width: 100, height: 50))

Solution 2

Create xcodeproject and add this view to ViewController's view and make sure this property works in native iOS project

func viewDidLoad() {
   super.viewDidLoad() 
   dnmTextField = DnmTextField(frame: UIScreen.main.bounds)
   view.addSubView(dnmTextField)
}

If it was not working, try to find the textField inside Debug View Hierarchy and if it wasnt there, there is a problem with adding textField to view's subviews

If not, the problem is about frame of the textField

Share:
237
TurkC
Author by

TurkC

Updated on December 26, 2022

Comments

  • TurkC
    TurkC over 1 year

    I am developing a flutter plugin. The library that I use with Swift has a UITextField subclass. I cannot show this subclass on the screen. It only appears on the screen when I try it with the UITextField object. How can I show this object on the screen?

    My Code

    dnmTextField = DnmTextField(frame: rc)
    dnmTextField?.autoresizingMask = .flexibleWidth
    dnmTextField?.layer.borderWidth = 1.0
    dnmTextField?.layer.borderColor = UIColor.black.cgColor
    dnmTextField?.clipsToBounds = true
    dnmTextField?.keyboardType = UIKeyboardType.numberPad
    dnmTextField?.textAlignment = .center
    dnmTextField?.backgroundColor = UIColor.black
    dnmTextField?.placeholder =  (args!["placeholder"] as! String)
    
    let presentedViewController = viewController?.presentedViewController
    let currentViewController: UIViewController? = (presentedViewController ?? viewController)
    currentViewController?.view.addSubview(dnmTextField!)
    

    Custom UITextField Subclass Reference

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @protocol DnmTextFieldDelegate <NSObject>
    
    @optional
    
    - (void) dnmTextFieldShouldBeginEditing:(NSInteger)typeId;
    
    - (void) dnmTextFieldDidBeginEditing:(NSInteger)typeId;
    
    - (void) dnmTextFieldDidEndEditing:(NSInteger)typeId;
    
    - (void) dnmTextFieldEditingChange:(NSInteger)typeId;
    
    - (void) dnmTextFieldDidClear:(NSInteger)typeId;
    
    @end
    
    @interface DnmTextField : UITextField
    - (void)setSystemId:(NSString*)systemId;
    - (void)setMaxLength:(NSUInteger)maxLength;
    - (void)setType:(NSInteger)typeId;
    - (BOOL)isEqualTo:(DnmTextField*)textField2;
    - (void)clear;
    - (BOOL)validateInput;
    - (BOOL)isEmpty;
    - (void)setSystemIdforGet:(NSString*)system_Id;
    - (void)setTextAlignment:(NSTextAlignment)textAlignment;
    - (NSInteger)getTextLength;
    
    @property (nonatomic, weak) id<DnmTextFieldDelegate> dnmDelegate;
    
    @property (nonatomic, assign) UIEdgeInsets edgeInsets;
    @property (nonatomic, assign) NSDictionary *attrDictionary;
    
    @property (nonatomic)  NSUInteger maxLength;
    
    
    @end
    
    
    #endif
    
    • Manish Punia
      Manish Punia over 3 years
      can you please show the implementation of class DnmTextField?
    • TurkC
      TurkC over 3 years
      It's a static library so I can't view its contents.
    • Manish Punia
      Manish Punia over 3 years
      Without looking into code we can not find the issue. I would suggest 1). looking into logs printed for that library. Look for any exceptions for that class. 2. Capture view Hierarchy, and check if the view is added or not. if added what is the frame for view.
    • Phil Dukhov
      Phil Dukhov over 3 years
      Could you provide a sample project with this problem? What does capturing view hierarchy says? Even of the outer code is the same, subclass may override layout methods which will lead to zero size
  • TurkC
    TurkC over 3 years
    Hello actually I'm sure the frame area is correct. Because when I change the DnmTextField object to UITextField everything works fine.
  • Damian Rzeszot
    Damian Rzeszot over 3 years
    @TurkC do you use the same constructor?