Is there a quick and easy way to resize a UIView after creating it and have its subviews resize also?
I have a custom UIView subclass called AnagramLetter, the implementation and interface code for which is shown below:
@interface AnagramLetter : UIView{
UILabel *letterLbl;
UIImageView *letterBG;
}
@property (nonatomic, retain) UILabel *letterLbl;
@property (nonatomic, retain) UIImageView *letterBG;
@end
@implementation AnagramLetter
@synthesize letterLbl,letterBG;
- (id)initWithFrame:(CGRect)frame
{
frame = CGRectMake(0, 0, 166, 235);
CGRect lblFrame = CGRectMake(1, 1, 164,164);
self = [super initWithFrame:frame];
[self setAutoresizesSubviews:YES];
if (self) {
// Initialization code
letterBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"anagram_correct_bg.png"]];
[self addSubview:letterBG];
letterLbl = [[UILabel alloc] initWithFrame:lblFrame];
letterLbl.backgroundColor = [UIColor clearColor];
letterLbl.textColor = [UIColor blackColor];
letterLbl.textAlignment = UITextAlignmentCenter;
letterLbl.numberOfLines = 0;
letterLbl.minimumFontSize = 50;
letterLbl.font = [UIFont boldSystemFontOfSize:144];
letterLbl.adjustsFontSizeToFitWidth = YES;
[self addSubview:letterLbl];
}
return self;
}
@end
When I press a button on my UI, I call a method which generate a list of the above items and populates a UIView along its X axis, creating a UIView for each character in a given string. Before I do this, I get the dimensions of the UIView (called anagramHolder), divided by the number of characters in the word. I then set the bounds/frame of the UIView after it has been initialized but so far there has been no change in the behaviour of the created AnagramLetter views.
Below is the code I use to get the dimensions, change the bounds of the created subclass and add the item to the anagramHolder UIView.
- (void) createAnagram {
float aWidth = 166.0;
float aHeight = 235.0;
CGRect rect = [anagramHolder bounds];
if (!anagramCreated){
for (int i=0; i<[word length]; i++) {
AnagramLetter *a;
if ((rect.size.width / [scrambled length]) < 166){
float percentDiff = ((rect.size.width / [scrambled length]) / 166);
aWidth = (rect.size.width / [scrambled length]);
aHeight = aHeight * percentDiff;
CGRect newBounds = CGRectMake(0, 0, aWidth, aHeight);
a = [[AnagramLetter alloc] initWithFrame:newBounds];
}
else { a = [[AnagramLetter alloc] init]; }
[a setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[a setAutoresizesSubviews:YES];
CGPoint pos;
pos.x = (i * (rect.size.width / [scrambled length])) + (aWidth/2);
pos.y = (rect.size.height/2);
[a setCenter:pos];
[a.letterLbl setText:[NSString stringWithFormat:@"%c",[scrambled characterAtIndex:i]]];
a.tag = i;
[anagramHolder addSubview:a];
[anagramLetters addObject:a];
[a release];
}
}
anagramCreated = YES;
[self getAnagramResult];
}
The easiest way to autoresize subviews of particular
viewis to set appropriate value ofsubviewsproperty namedautoresizingMask.For example,
From Apple Docs:
More info can be found here: UIView
P.S. Also I am using
autoresizingMaskto design my applications both for iPhone and iPad.