How to Call drawRect programmatically in objective c
Asked Answered
G

5

23

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

Gentlewoman answered 4/5, 2010 at 10:33 Comment(2)
Why would you want to draw a view without actually putting it on the screen? Also, you should never call drawRect: directly (except for super).Furniture
Suppose i din't select First tab ,after app launch. From Second tab selection i want to show the first tab with the content changes.Content changes means i have to change the UITextfield etc. Now what is happening is at first time my selected tab's textfield not changing. But wen i select again it will change.Gentlewoman
S
44
[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?

Swaney answered 4/5, 2010 at 12:57 Comment(7)
There's a case where you do want to render it off screen, and that is when you intend to use the view's content (e.g. take a screenshot of it, or such) and cache or use it elsewhere. In my case, I want to take a screenshot of a view that isn't necessarily seen by the user and save that view's image to disk.Epley
I'm not sure I understand this solution. UIView doesn't have a display method.Twentieth
I believe he means [myView.layer display].Basketball
Yes, he probably means [myView.layer display]. This should not be called directly though. Apples reference for CALayer display: "Do not call this method directly. The layer calls this method at appropriate times to update the layer’s content"Boner
Also, setNeedsDisplay does not take any arguments.Boner
@IulianOnofrei Ha ha, It must have existed in 2010 or the answer would not have been accepted.Swaney
setNeedsDisplay might be useful if the view that needs to be updated manage the layout with hard coded value, for example if your view is rotating in landscape mode, then you might need to update the layout. If you are using constraints (auto layout), you should be fine.Role
D
4

setNeedsDisplay

Depersonalize answered 4/5, 2010 at 10:43 Comment(1)
Thanks for answer. But setNeedsDisplay will call only if the view is visible. In my condition view will not be visible. Sorry. my question was misleading. i have edited my questionGentlewoman
R
3

setNeedsDisplayInRect:

Rentsch answered 4/5, 2010 at 10:52 Comment(0)
E
1

Swift:

yourView.setNeedsDisplay()

or

yourView.setNeedsDisplay(newRect)
Earleneearley answered 16/8, 2017 at 9:22 Comment(0)
H
0

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.

Handset answered 20/10, 2014 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.