【问题标题】:resize uiview subclass after creation创建后调整uiview子类的大小
【发布时间】:2012-02-23 03:54:57
【问题描述】:

是否有一种快速简便的方法可以在创建 UIView 后调整其大小并使其子视图也调整大小?

我有一个自定义的 UIView 子类,叫做 AnagramLetter,它的实现和接口代码如下所示:

@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];
}

【问题讨论】:

    标签: iphone objective-c uiview subclass anagram


    【解决方案1】:

    自动调整特定view 的子视图大小的最简单方法是设置名为autoresizingMasksubviews 属性的适当值。

    例如,

    UIView *view;
    view.autoresizesSubviews = YES;
    UIView *subview;
    subview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    [view addSubview:subview];
    

    来自 Apple 文档:

    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;
    

    更多信息可以在这里找到:UIView

    附:我还使用autoresizingMask 来设计我的 iPhone 和 iPad 应用程序。

    【讨论】:

    • 感谢您的评论,我已经在我的 AnagramLetter.m 文件和我的主要实现文件中尝试了您上面的代码,但仍然没有效果。知道我可能做错了什么吗?
    • 在我的实现文件中,在添加 letterBG 子视图之前,我添加了以下代码:letterBG.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; 同样,我在添加 letterLbl 作为子视图之前添加了 letterLbl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    • 试试其他掩码组合。
    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 2014-09-02
    • 2013-04-12
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2014-12-29
    相关资源
    最近更新 更多