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

S.P.
  • 5,427
  • 11
  • 56
  • 83
  • 4
    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`). – bddckr May 04 '10 at 12:00
  • 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. – S.P. May 07 '10 at 10:42

5 Answers5

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?

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • 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. – Kalle Oct 25 '10 at 17:06
  • 2
    I'm not sure I understand this solution. `UIView` doesn't have a `display` method. – Slipp D. Thompson Dec 31 '12 at 04:27
  • 2
    I believe he means `[myView.layer display]`. – Aaron Brager Jan 22 '14 at 17:30
  • 1
    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" – LoPoBo Mar 19 '14 at 10:40
  • Also, `setNeedsDisplay` does not take any arguments. – LoPoBo Mar 19 '14 at 10:44
  • @IulianOnofrei Ha ha, It must have existed in 2010 or the answer would not have been accepted. – JeremyP May 22 '17 at 09:02
  • 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. – Oleg G. Mar 01 '19 at 21:12
4

setNeedsDisplay

ohho
  • 50,879
  • 75
  • 256
  • 383
  • 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 question – S.P. May 04 '10 at 10:55
3

setNeedsDisplayInRect:

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
1

Swift:

yourView.setNeedsDisplay()

or

yourView.setNeedsDisplay(newRect)
Hemang
  • 26,840
  • 19
  • 119
  • 186
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.

return true
  • 7,839
  • 2
  • 19
  • 35