1

I was wondering if it is possible to create an NSRect with maybe an NSMakeRect to make a simple square that will display on the screen without a window or any view behind it, just made all in code.

This is what I have as an example

-(void)drawRect
{
   NSRect myNewRect
   myNewRect = NSMakeRect(100, 100, 50, 50);
}

Thats just a simple starting point but it will not show up on the screen by itself. what do i need to add?

Thanks

Grant Wilkinson
  • 1,088
  • 1
  • 13
  • 38

2 Answers2

2

Every drawing operation on Mac OS X requires some window of sort. So no, you cannot draw a rect without a window. But you can create a transparent window without any borders to draw into.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
1

First of all, you can't "display on the screen without a window or any view behind it".

You will always be drawing on some layer-backed object (UIView, etc).

And UIViews must eventually be part of some UIWindow hierarchy to display them.

So you can't "[draw] on main screen without window" at all. That's not how Core Graphics works.

However, I believe this is what you're trying to do:

-(void)drawRect
{
   CGRect myNewRect = CGRectMake(100, 100, 50, 50);

   CGContextRef ctx = UIGraphicsGetCurrentContext();
   CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);
   CGContextFillRect(ctx, myNewRect);
}

Which draws a rectangle in the UIView implementing the above drawRect method.

Steve
  • 31,144
  • 19
  • 99
  • 122