0

I'm initting a UIView with a xib, programatically:

- (id)initWithFrame:(CGRect)frame
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"MyViewNib" owner:self options:nil] objectAtIndex:0];
    if (self)
    {
        //perform setup of various components

        return self;
    }
}

This view uses size classes for Any Width, Any Height, and Any Width, Compact Height.

I have a UIButton in a xib that I need to put a circle in the background of, like so:

self.closeButton.layer.cornerRadius = self.closeButton.frame.size.width / 2.0f;
self.closeButton.layer.backgroundColor = [UIColor colorWithWhite:164.0f / 255.0f alpha:1.0f].CGColor;

If I try setting the button's corner radius in the init function when I'm on a device using Any Width, Compact Height, the label's frame is still set to the Any Width, Any Height value. I've also tried overriding layoutSubviews and setting the value there, with no luck. It appears that the size class constraints are applied after layoutSubviews without another call to layoutSubviews, since the other components appear on-screen correctly.

So, I'm wondering if there's a good entry point for me to catch where the size classes are applied, so that I can set the background corner radius of the button correctly. I could just set the button up programmatically, but I'd like to figure out how to do this since it will probably come up again in the process of converting to size classes.

5 Answers 5

0

In your xib class -(void)awakeFromNib;

1
  • It looks like awakeFromNib is called as soon as the Xib loading function is called, so even before the "if (self)" init body. Jul 8, 2015 at 17:06
0

This works if I put it at the end of the init function:

 [self performSelector:@selector(updateButtonBackground) withObject:nil afterDelay:.001];

Apparently by this point the constraints have been set up with the size classes. It's certainly non-ideal though, so if anyone has an answer that's event-driven or involves view "lifecycle" (as far as that applies to UIView) I'll accept that...

0

How about doing this?

-(void)layoutSubviews
{
    [super layoutSubviews];
self.closeButton.layer.cornerRadius = self.closeButton.frame.size.width / 2.0f;
self.closeButton.layer.backgroundColor = [UIColor colorWithWhite:164.0f / 255.0f alpha:1.0f].CGColor;
//more of your changes
[self layoutIfNeeded]; // You can remove this line if it works without it for you..
}
1
  • No luck there; it still sets the corner radius based on the Any, Any screen size... Jul 10, 2015 at 17:27
0

I know this thread is old, but I see no good answer here.

Size classes are defined in traitCollectionDidChange: method. You can't ensure that traitCollection is set in initWithFrame: or awakeFromNib:. So set your corner radius in traitCollectionDidChange: method based on the view size classes.

-1

You need to apply the corner radius in viewDidLayoutSubviews. It is at this point that you know the bounds of the button have been established.

You can see an example on my blog.

4
  • This is for a programmatically initialized view from a xib, and putting it in viewDidLayoutSubviews doesn't seem to work... Oct 1, 2015 at 16:54
  • You mention that you are doing size classes for Any Width, Any Height, and Any Width, Compact Height. This would mean the only time you'll see a difference from Any/Any is with any of the iPhones in landscape mode. Are you launching in portrait on an iPhone and then rotating to landscape and viewDidLayoutSubviews isn't working in that case? Oct 1, 2015 at 19:30
  • No rotation, it seems the issue is that the view is set up at Any, Any size even in/after viewDidLayoutSubviews... Smells like a bug to me! Oct 2, 2015 at 15:36
  • It will still be Any to Any after viewDidLayoutSubviews unless you rotate it to landscape or are launching in landscape, based on your size classes. It would be interesting to see what happens with a size class for Compact Width/Any Height so that it should be sized correct in Portrait. Oct 2, 2015 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.