How to Call drawRect programmatically in objective c - iphone

How to Call drawRect programmatically in objective c ?
I want to call drawrect method of a view in my UItabbarcontroller. How i can do this ? Thanks in advance..
Edit
I have to call when the view is not visible currently. It will be the first time i have to call that view

[myView.layer display];
Forces the view to draw itself straight away.
[myView setNeedsDisplay: YES];
Forces the view to redraw on the next event loop cycle.
However, if you need to call it even when it is not visible, I think there is something wrong with the design of your view class. You should only be doing drawing inside drawRect: not anything else. And if you are only doing drawing why do it when the view is not visible?

setNeedsDisplay

setNeedsDisplayInRect:

Swift:
yourView.setNeedsDisplay()
or
yourView.setNeedsDisplay(newRect)

view.layer.renderInContext(context: CGContext) can draw the content of your view into a CGContext, e.g. a bitmap.
view.snapshotViewAfterScreenUpdates(afterUpdates: Bool) gives you a snapshot of the view.

Related

Using drawInRect on a view?

I have a drawInRect method which I wish to use on another UIView, a subview of this view.
Is there a way to do this?
If you are interested in duplicating the drawing, you could put your drawing in a separate function (drawFunct) of the top level view, and just call [self.superview drawFunct] from within the subviews drawInRect.
Calling the drawInRect is generally discouraged.

drawRect causes slow scrolling even though it is not being called

In my app I have a scrollView with about 20 subviews in it. Each of these subviews has a drawRect method that at the moment looks like this:
- (void)drawRect:(CGRect)rect
{
NSLog(#"drawRect called");
}
When the subviews are added, drawRect is called, however when I scroll it is very slow even though drawRect is not called again.
If I remove the implementation for drawRect, then scrolling is completely normal. Even if I can't get rid of the slow scrolling, is there an alternative to drawRect which I could use instead?
Why are you calling drawRect if it is only logging that it was called? If that is it's only purpose for you, then just don't call it. In fact, I believe when you first create a class that inherits from UIView that has the drawRect method in it, it is commented out and above the commented out drawRect method, it says something along the lines of "Do not call this method if it does not do any drawing on screen as it takes up a significant amount of memory". Basically, don't call it in your case.
Hope this helps.
You could try assigning pre-drawn CGImages to the contents of each custom view's CALayer, before the view first appears. This may be faster than using a drawRect to customize a view's appearance.

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.

Infinite loop when adding CATiledLayer to UIView

I have a UIView in which I add a CATiledLayer and implement 'drawLayer'.
If I use a UIViewController and add the layer to a new subview of the controller, then everything is ok.
If I however try to use a UIView to and do all the craetion and drawing within this, then I get a infinite loop at the point shown below when I add this view to a superview.
0x002cfafb <+0425> ja 0x2cfa23 <-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]+209>
What am I missing?
Update:
By removing 'layer.delegate = self', the code no longer crashes...
Just found this:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/18121-calayer-interesting-crash.html
"You can't set the delegate to be the same as the view's layer's delegate, which by default is the view itself"

Render data offscreen in UIView

How would I go about drawing a view offscreen in order to perform some calculation before the view's draw is actually done? Can this be done by drawing to a temporary layer?
I am not getting your exact problem... But you can use
-(void)viewWillAppear:(BOOL)animated
this method will be called every time before your view will appear on the screen... you can do the calculation and make some changes here before view appear on the screen...