2012-01-31 30 views
2

UIViewの作成後にサイズを変更し、サブビューのサイズを変更することも、すばやく簡単にできますか?作成後にサイズ変更の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上のボタンを押すと、私はのリストを生成するメソッドを呼び出しますUIViewをX軸に沿って読み込んで、指定された文字列内の各文字の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]; 
} 

答えて

3

view特定のサブビューを自動サイズ変更する最も簡単な方法は、autoresizingMaskという名前subviewsプロパティの適切な値を設定することです。例えば

、Appleのドキュメントから

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

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

P.S.また、私はautoresizingMaskを使ってiPhoneとiPadの両方のアプリケーションを設計しています。

+0

あなたのコメントのおかげで、私は私のAnagramLetter.mファイルと私の主な実装ファイルの両方で上記のコードを試しましたが、まだ効果はありません。私は間違って何をしているのだろうか? –

+0

私の実装ファイルでは、letterBGサブビューを追加する前にこのコードを追加しました: 'letterBG.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; '同様に、私は' letterLbl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |を追加しました。 | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; 'サブビューとしてletterLblを追加する前に、 –

+0

マスクの別の組み合わせを試してみてください。 – Nekto

関連する問題