简体   繁体   中英

UITextField not showing when added as subview

I'm making an OpenGL ES application and I am trying to work out how to incorporate parts of the UIKit GUI over the view with the OpenGL ES working on it.

In the init method of the EAGLView class I have this to setup the UITextField :

add_name_field = [[UITextField alloc] initWithFrame: CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width, 50)];

Somewhere in the drawRect method where everything is rendered and processed for the game I have:

[add_name_field becomeFirstResponder];
[self insertSubview: add_name_field atIndex:0];

I can confirm this code is called because I used NSLog to test that but when it is run the text field does not show over the game's OpenGL view. If I put the code in the init method, the text field shows properly with the keyboard at te beginning of the game.

So how do I get it to work in the drawRect method?

Thank you for any answer. I will write some code for the delegate in the meantime.

THank you for all the other answers but I found the solution.

It was a simple solution. I simply moved the addSubView method to the initialisation of the EAGLView class. The keyboard wont show until I make the text field the first responder. When I'm finished with it, I can simply clear the text and make the EAGLView class the first responder again and that hides it all again.

This way I can simply use the keyboard to get text input for my game, displaying text when typing and hiding it afterwards.

I assume you're in a UIViewController subclass, with the code above? If so, then instead of

[self insertSubview: add_name_field atIndex:0];

do:

[self.view addSubview:add_name_field];

The important part is, you're calling this method on self.view rather than self . Adding subviews is a VIEW function, not a View CONTROLLER function.

insertSubview:atIndex: ought to work too (if called on the right object), but addSubview: will stick it on top of the view no matter what else is there, so it's a bit cleaner.

Add your text subview outside of drawRect. drawRect is for rendering that one view, and may or may not ignore drawing any other views (such as your text view), depending on how the view stack is rendered.

Is there any reason to have it done in the drawRect method? You said that it works fine in the init method, so why not have it there?

As for why it doesn't work, I don't exactly know OpenGL views but, assuming they work just like regular UIViews, you probably shouldn't be adding other UIViews in the drawRect function. UIViews don't get shown & drawn immediately; they have to wait till the runloop tells them to draw. Likely what happens is that when your superview's drawRect finishes, it assumes all its subviews have finished drawing and doesn't realized that your newly added textfield didn't get a chance to. What you could try is calling setNeedsDisplay on your textfield.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM