是否有一种快速简便的方法可以在创建UIView并调整其子视图大小后调整其大小?
我有一个名为AnagramLetter的自定义UIView子类,其实现和接口代码如下所示:
@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
当我按下UI上的按钮时,我调用一个生成上述项目列表的方法,并沿其X轴填充UIView,为给定字符串中的每个字符创建一个UIView。在我这样做之前,我得到了UIView的尺寸(称为anagramHolder),除以单词中的字符数。然后我在初始化之后设置UIView的边界/框架,但到目前为止,创建的AnagramLetter视图的行为没有变化。
下面是我用来获取维度的代码,更改创建的子类的边界并将项目添加到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];
}
答案 0 :(得分:3)
自动调整特定view
子视图的最简单方法是设置名为subviews
的{{1}}属性的适当值。
例如,
autoresizingMask
来自Apple Docs:
UIView *view;
view.autoresizesSubviews = YES;
UIView *subview;
subview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
[view addSubview:subview];
可以在此处找到更多信息:UIView
P.S。此外,我正在使用UIViewAutoresizing Specifies how a view is automatically resized.
enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5 };
typedef NSUInteger UIViewAutoresizing;
为iPhone和iPad设计我的应用程序。