Issue in UITextField add inside UIImageView in iPh

2019-04-17 10:28发布

问题:

Currently i am working in iPhone app, Using UIImageView to set a background image on the screen, then add UITextField inside the UIImageView, then i run the app, the UITextField cannot enabled, How to enable this? please help me

Thanks in Advance

I tried this:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.image=[UIImage imageNamed:@"2ndscreenback.png"];

Daystext =[[UITextField alloc]initWithFrame:CGRectMake(57, 122, 171, 31)];
Daystext.borderStyle=UITextBorderStyleRoundedRect;
Daystext.delegate=self;
Daystext.userInteractionEnabled=YES;
[imageView addSubview:Daystext];
[self.view addSubview:imageView];

回答1:

Just add below line in your code

imageView.userInteractionEnabled = YES;

Means like this

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.image=[UIImage imageNamed:@"2ndscreenback.png"];

Daystext =[[UITextField alloc]initWithFrame:CGRectMake(57, 122, 171, 31)];
Daystext.borderStyle=UITextBorderStyleRoundedRect;
Daystext.delegate=self;
Daystext.userInteractionEnabled=YES;
imageView.userInteractionEnabled = YES;
[imageView addSubview:Daystext];
[self.view addSubview:imageView];


回答2:

try like this,

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.image=[UIImage imageNamed:@"2ndscreenback.png"];
imageView.userInteractionEnabled=YES;
[self.view addSubview:imageView];

Daystext =[[UITextField alloc]initWithFrame:CGRectMake(57, 122, 171, 31)];
Daystext.borderStyle=UITextBorderStyleRoundedRect;
Daystext.delegate=self;

[imageView addSubview:Daystext];

you can get the textfield on imageview....



回答3:

Try this:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.image=[UIImage imageNamed:@"2ndscreenback.png"];

Daystext =[[UITextField alloc]initWithFrame:CGRectMake(57, 122, 171, 31)];
[self.view addSubview:imageView];
Daystext.borderStyle=UITextBorderStyleRoundedRect;
Daystext.delegate=self;
Daystext.userInteractionEnabled=YES;
[self.view addSubview:Daystext];


回答4:

I think it's better to set background image for your UITextFild like this:

myTextField.borderStyle = UITextBorderStyleNone;
myTextField.background = [UIImage imageNamed:@"back.png"];

or like this

UIImageView *myView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"back.png"]];
[myTextField  setRightView:myView];
[myTextField  setRightViewMode:UITextFieldViewModeAlways];


回答5:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.image=[UIImage imageNamed:@"2ndscreenback.png"];

Daystext =[[UITextField alloc]initWithFrame:CGRectMake(57, 122, 171, 31)];
[self.view addSubview:imageView];
Daystext.borderStyle=UITextBorderStyleRoundedRect;
Daystext.delegate=self;
Daystext.editable = YES;
Daystext.userInteractionEnabled=YES;
[self.view addSubview:Daystext];

Try this. Hope it works for u !!!