I'm creating a UIView from a xib to eventually render into a UIImage.
The UIView itself has internal constraints which work fine given a large enough frame size in IB. However, I need to size the width to some particular content, and I can't figure out how to set the width of the UIView after loading it from a xib.
[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil][0];
To render the view, I do this:
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
No matter what I set the bounds to, however, the UIView is the same size as the initial size in IB. How can I set the size of the UIView such that it will cause internal constraints to resize and render correctly?
UIView
delays its internal update after the frame change to the next run loop? I've run into problems with this before, and wrapping subsequent rendering code in adispatch_async
on the main thread usually fixed it (but then again, setting width constraints programmatically also always worked for me, so you might have something else causing the problem). – Greg