The is the first of several problems I’m having setting up some UIViews and subviews. I have a UIView that is dynamically positioned on screen at run time. That UIView (master) contains another UIView (child) which wraps a UIImageView and a UILabel. Here are the requirements I have for this arrangement:
- The child
UIViewmust stay centered in the masterUIViewwhen the device rotates. - The text in the
UILabelcan be very long or very short and the childUIViewwith the image and text must still remain centered. - I would like to avoid subclassing UIView to handle this scenario and I would also like to avoid any frame/positioning code in
willRotateToInterfaceOrientation.I’d like to handle all of this with someautoresizingMasksettings in I.B. and maybe a little forced resizing code, if possible.
This is the arrangement of controls in Interface Builder(highlighted in red):

With Interface Builder, the autoresizingMask properties have been set like so, for the described controls
UIView(master): Flexible top margin, Flexible left margin, Flexible right margin, Flexible widthUIView(child): Flexible top margin, Flexible bottom margin, Flexible left margin, Flexible right margin, Flexible width, Flexible height. (All modes, except None)UIImageView: Flexible right marginUILabel: Flexible right margin
This is the view (red bar with image and text) after it’s been added programmatically at run time while in portrait mode:
The master UIView‘s background is a light-red colored image. The child UIView‘s background is slightly darker than that, and the UILabel‘s background is even darker. I colored them so that I could see their bounds as the app responded to rotation.

It’s clear to me that:
- It is not centered but …
- After changing the text from it’s default value in I.B from “There is no data in this map extent.” to “TEST1, 123.” the label contracts correctly.
This is the view after it’s been added while in portrait and then rotated to landscape mode:

From here I can see that:
- It is still not centered and perhaps at its original frame origin prior to rotation
- The UIView (child) has expanded to fill more of the screen when it shouldn’t.
- The UIView (master) has properly expanded to fill the screen width.
This is the code that got me where I am now. I call the method showNoDataStatusView from viewDidLoad:
// Assuming
#define kStatusViewHeight 20
- (void)showNoDataStatusView {
if (!self.noDataStatusView.superview) {
self.noDataStatusView.frame = CGRectMake(self.mapView.frame.origin.x,
self.mapView.frame.origin.y,
self.mapView.frame.size.width,
kStatusViewHeight);
self.noDataStatusView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgRedStatus.png"]];
// Position the label view in the center
self.noDataStatusLabelView.center = CGPointMake(self.noDataStatusView.frame.size.width/2,
self.noDataStatusView.frame.size.height/2);
// Test different text
self.noDataStatusLabel.text = @"Testing, 123.";
// Size to fit label
[self.noDataStatusLabel sizeToFit];
// Test the status label view resizing
[self.noDataStatusLabelView resizeToFitSubviews];
// Add view as subview
[self.view addSubview:self.noDataStatusView];
}
}
Please note the following:
resizeToFitSubviewsis a category I placed on UIView once I found that UIView’s won’t automatically resize to fit their subviews even when you callsizeToFit. This question, and this question explained the issue. See the code for the category, below.- I have thought about creating a UIView subclass that handles all this logic for me, but it seems like overkill. It should be simple to arrange this in I.B. right?
- I have tried setting every resizing mask setting in the book, as well as adjusting the order in which the resizing of the label and view occur as well as the point at which the master view is added as a subview. Nothing seems to be working as I get odd results every time.
UIView resizeToFitSubviews category implementation method:
-(void)resizeToFitSubviews
{
float width = 0;
float height = 0;
// Loop through subviews to determine max height/width
for (UIView *v in [self subviews]) {
float fw = v.frame.origin.x + v.frame.size.width;
float fh = v.frame.origin.y + v.frame.size.height;
width = MAX(fw, width);
height = MAX(fh, height);
}
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, width, height)];
}
What I want to know is why the UIView (child) is not properly centered after it’s superview is added to the view hierarchy. It looks as though its got the proper width, but is somehow retaining the frame it had in I.B. when the label read “There is no data in this map extent.”
I want to also know why it’s not centered after device rotation and whether or not the approach I’m taking here is wise. Perhaps this is causing the other issues I’m having. Any UIView layout help here would be greatly appreciated.
Thanks!
The problem here is that my
UIView(master) does not layout it’s subviews automatically when the device rotates and the “springs & struts” layout method used to position the image and interiorUIViewwas inefficient. I solved the problem by doing two things.UIView(child) instance, leaving only theUIView(master) and inside of that aUILabelandUIImageView.UIViewsubclass calledStatusViewand in it I implement thelayoutSubviewsmethod. In its constructor I add aUIImageViewandUILabeland position them dynamically. TheUILabelis positioned first based on the size of the text and then theUIImageViewis placed just to the left of it and vertically centered. That’s it. InlayoutSubviewsI ensure that the positions of the elements are adjusted for the new frame.Additionally, since I need to swap the background, message and possibly the image in some circumstances, it made sense to go with a custom class. There may be memory issues here/there but I’ll iron them out when I run through this with the profiling tool.
Finally, I’m not totally certain if this code is rock solid but it does work. I don’t know if I need the layout code in my init method, either. Layout subviews seems to be called shortly after the view is added as a subview.
Here’s my class header:
… and implementation:
Then in my view controller I can do this:
… and later on I can change it up by doing this:
Now I’ve got a reusable class rather than 12 UIView instances floating around my NIB with various settings and properties.