Ivan Trubnikov
Ivan Trubnikov

Reputation: 177

CGRect automatically resize after Action

I have some code in separate class, who bounded with UIView.

CGContextRef context = UIGraphicsGetCurrentContext();

CGRect drawRect = CGRectMake(20, 262, 139, 169);

[self setFrame:drawRect];

CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [[UIColor colorWithRed:125.0f/255.0f green:251.0f/255.0f blue:181.0f/255.0f alpha:1] CGColor]);
CGContextSetRGBFillColor(context, 200.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f);

CGContextFillRect(context, drawRect);

But after clicking this view, size of UIView resizes to dimensions from interface builder model from my storyboard.

What does matter, and what's wrong with it?

Upvotes: 0

Views: 112

Answers (1)

Rob Napier
Rob Napier

Reputation: 299275

drawRect: is not the place to change the frame. If the system can't automatically handle resizing the frame (by scaling the view, for instance), then drawRect: is called in response to resizing the frame. If you could change the frame size in the middle of drawing, you'd have an infinite loop.

If you want the view to be in the rectangle (20,262,139,169), then that's simple to put in autolayout. Just put the view where you want it, and create constraints to keep it there using Interface Builder.

If you don't want autolayout (not sure why you'd want to turn it off here, but if you did), you can turn it off and call setFrame: in viewDidLoad. But there's no reason to call it again in drawRect:.

Upvotes: 1

Related Questions