Tuesday 15 June 2010

ios - UIView not setting background color in drawRect -


I have created a square that is UIView subclasses. In DrawRect, I have an orange color rectangle, but it looks as black.

In my custom class:

  #import "testRect.h" @ calculation test - (id) initWithFrame: (CGRTAT) frame {self = [super initWithFrame: frame ]; If (self) {} returns back to himself; } - (Zero) Direct: (CGRact) Rect // Color Announcements UICLOR * Stroke Color = [UIcolor Color Withride: 0 Green: 0 blue: 0 alpha: 1]; UIColor * fillcolor2 = [UIColor colorWithRed: 0.886 green: 0.59 blue: 0 alpha: 1]; //// Rectangle drawing UIBezierPath * rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake (26.5, 171.5, 265, 139)]; [Filler 2 settings]; [Fill in rectangle]; [Strokecollar setstroke]; RectanglePath.lineWidth = 1; [Rectangle stroke]; }  view controller  ViewController - View (zero) viewDidload {[Super Viewedload]; // Setup an additional after loading the view, usually from a nibb. TestRect * test = [[testrect alloc] initWithFrame: CGRactack (10, 10, 265, 139)]; [Self.view addSubview: test]; } - (zero) did risiv memorialisation [[super daviditamammy warning]; // Any resource settlement which can be reproduced. } @end    

used the CGRect in your code Has been drawn> Drawing: The method is wrong You should have a CGRect which is relative to that view. This means that the original will be 0,0. Also, calculate on the runtime, depending on the limit of viewing of the rake. Do not hardcode it in drawRect: .

You now have a y-origin of 171.5 in the reset ( drawRect: that you use, but see that the height is only 139. So whatever you do with the drawing It is outside the scene, that is why it looks black.

You probably want:

  cgartact bound = self.bounds; UIBezierPath * rectanglePath = [ UIBezierPath bezierPathWithRect: bounds];    

No comments:

Post a Comment