Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing outside a UIView

Tags:

ios

uiview

I have a UIView where I would like to draw a Circle that extends past the frame of the UIView, I have set the masksToBounds to NO - expecting that I can draw past outside the bounds of the UIView by 5 pixels on the right and bottom.

I expect the oval to not get clipped but it does get clipped and does not draw outside the bounds?

- (void)drawRect:(CGRect)rect
{

    int width = self.bounds.size.width;
    int height = self.bounds.size.height;
    self.layer.masksToBounds = NO;

    //// Rounded Rectangle Drawing
    //// Oval Drawing
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(0, 0, width+5, height+5)];
    [[UIColor magentaColor] setFill];
    [ovalPath fill];
    [[UIColor blackColor] setStroke];
    ovalPath.lineWidth = 1;
    [ovalPath stroke];

}

enter image description here

like image 776
OneGuyInDc Avatar asked Feb 18 '13 23:02

OneGuyInDc


1 Answers

From http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/DrawingModel.html

UIView and NSView automatically configure the drawing environment of a view before its drawRect: method is invoked. (In the AppKit framework, configuring the drawing environment is called locking focus.) As part of this configuration, the view class creates a graphics context for the current drawing environment.

This graphics context is a Quartz object (CGContext) that contains information the drawing system requires, such as the colors to apply, the drawing mode (stroke or fill), line width and style information, font information, and compositing options. (In the AppKit, an object of the NSGraphicsContext class wraps a CGContext object.) A graphics context object is associated with a window, bitmap, PDF file, or other output device and maintains information about the current state of the drawing environment for that entity. A view draws using a graphics context associated with the view’s window. For a view, the graphics context sets the default clipping region to coincide with the view’s bounds and puts the default drawing origin at the origin of a view’s boundaries.

Once the clipping region is set, you can only make it smaller. So, what you're trying to do isn't possible in a UIView drawRect:.

like image 200
escrafford Avatar answered Nov 01 '22 07:11

escrafford