TurkC
TurkC

Reputation: 425

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

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

Upvotes: 2

Views: 314

Answers (2)

Mohammad Reza Koohkan
Mohammad Reza Koohkan

Reputation: 1734

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

Upvotes: 0

Damian Rzeszot
Damian Rzeszot

Reputation: 555

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

Upvotes: 0

Related Questions