UIView subclass and AutoLayout

Discussion in 'iOS Programming' started by dantastic, Jan 15, 2016.

  1. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    I have a UIImageView subclass which contains a load of rendering code to render images. This class uses IBInspectable to set the values required through IB. Before AutoLayout this was working great like so

    Code:
    - (void) awakeFromNib {
        UIImage *image = [self MyCoolImageRenderWithSize:self.frame.size];
        self.image = image;
    }
    But I found when using this component with Size Classes in IB the rendered image is no longer right. Aaah, The frame size is not right. I have to resort to this hack

    Code:
    - (void) awakeFromNib {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = [self MyCoolImageRenderWithSize:self.frame.size];
            self.image = image;
        });
    }
    To get the image after the frame has been set properly. This isn't great as you can see the screen for a fraction of a second before the image is rendered.

    So the question is, in a UIView subclass, what method is called as Autolayout is updating the view?

    I have tried layoutSubviews, drawrect:, layoutMarginsDidChange, setFrame:, didMoveToSuperview... These are either not called or called before the final frame is set.

    So what am I missing? What method is called on the UIView after each autolayout pass?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  3. dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    I appear to have the correct frame size in updateConstraints but the code to render the image produce an image the wrong size. I have tracked the sizing issue down to a method as part of the image generation. I don't know why exactly but the Core Graphics Bitmap context is not able to distinguish pixels with alpha only in the same runloop as the view is being set up. This is a bit more complicated question and although this was working fine before autolayout I don't think it has anything to do with the autolayout configuration as such.

    I'll look at the CG code and see if I can find a workaround.
     

Share This Page