views:

874

answers:

2

I have a subclass of UIView that I've added to as the titleView of a navigationItem using the following line of code:

self.navigationItem.titleView = tempview;

Easy enough. That works fine. My problem is that this navigationItem sometimes has the rightBarButton updated (sometimes there is no button, sometimes there is one standard sized button, sometimes there is a larger button).

I figured that I could simply use the layoutSubviews method of the tempview class that I've added as the titleView so I put this in:

-(void)layoutSubviews {
    [super layoutSubviews];
    self.mylabel.frame = self.bounds;
}

This does not seem to work, as it does not correctly resize the titleview when the rightBarButton item is updated.

I've noticed also that the bounds do not grow once they gotten smaller, they simply change the position.

I've tried using setNeedsLayout and layoutIfNeeded but those simply "resize" the view with the incorrect bounds.

I've also made sure that the rightBarButton item is set to nil, but the view still does not correctly expand once shrunk.

Thanks for any help!

A: 

Instead of overriding -layoutSubviews have you tried setting the autoresizingMask property?

tempView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
moshy
I tried that and it doesn't seem to do anything different than the `layoutSubviews` approach. Thanks though!
mjdth
+2  A: 

By default, layoutSubviews does nothing. You also never change the size of the titleView, so unless a navbar does that for you, it's no surprise that nothing is changing.

Since you're only changing the size of one of the subviews, I don't think autoresize will work, as I'm pretty sure it's triggered when the superview (the one with autoresizesubviews enabled) changes size. I would suggest recalculating the size of the titleview when you change the rightbutton. If it automatically fits when you add it the first time, you could remove and readd it, but I know that's a pretty ugly hack.

David Kanarek