2016-08-05 10 views
0

私はthis SO link for autoresizingに従っていますが、自動サイズ調整は機能しません。
自動サイズ設定でプログラムでフレームを設定するにはどうすればよいですか?UIViewの自動サイズ設定がプログラムで追加されていません

私はiPhone 4

UIView *box = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)]; 
[box setBackgroundColor:[UIColor redColor]]; 

[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; 
[box setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
[box setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin]; 
[box setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; 
[box setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; 

[self.view addSubview:box]; 

のためのフレームを設定している。しかし、最初の6

+0

マスクのサイズを変更する代わりに、制約を使用しないのはなぜですか? – Konstantin

+0

UI.iのストーリーボードをプログラムで使用していないので@Konstantin –

+0

UIの作成方法は問わず、コードの制約を使用することはできます。 – Konstantin

答えて

0

まず最初にiPadの中で仕事やiPhoneされていません。オープンの提案を1として、あなたは制約を使用する必要があります。

自動認識マスクを追加した後に追加して表示します。

UIView *box = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)]; 
[box setBackgroundColor:[UIColor redColor]]; 
[self.view addSubview:box]; 

[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; 
[box setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
[box setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin]; 
[box setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; 
[box setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; 
+0

返信ありがとうKaushal、私はこのコードを試してみましたが、私のために働いていません –

+0

マスクの列挙型は適切ではない、固定幅が必要な場合は、高さはUIViewAutoresizingFlexibleHeight、UIViewAutoresizingFlexibleWidthを使用しないでください。現在のシナリオでは、フレームはゼロになります。 – kaushal

0

私はあなたが正しくあなたが高さ= 120

ADDのEXPLANATION

で一番上に赤いビューを設定しようとしている質問を理解している場合は、制約を使用してそれを達成することができます:

 UIView *box = [[UIView alloc] init]; 
// Prevent creating constraints from masks automatically 
     box.translatesAutoresizingMaskIntoConstraints = NO; 
     box.backgroundColor = [UIColor redColor]; 
     [self.view addSubview:box]; 

// Define metrics (constants) which will be used to create constraints. 
// Key @"boxSize" - name which will be used in constraints, Value - constant 
     NSDictionary *metrics = @{@"boxSize" : @(120)}; 

// Define views that will participate in auto layout constraints. 
// Key @"readBox" - name which will be used in constraints, Value - real UIView object 
     NSDictionary *views = @{ @"redBox" : box }; 


// Here we create constraints. For Vertical, and for Horizontal 

// I'm using Visual language format (you can find it in Apple Documentation 
// In a few words: 
// H:|-0-[redBox]-0-| 
// "H" - means horizontal 
// "|" - short cut for parent view (in our case it is UIViewController.view) 
// "[redBox]" - view name from view's dictionary 
// "-0-" - gap between views (you could set number), in our case it is "|" and "[redBox]" 
// "[redBox(boxSize)]" - means that view (redBox) size should be qual to "boxSize" from metrics' dictionary 


     [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[redBox]-0-|" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]]; 

     [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[redBox(boxSize)]" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]]; 

Apple Documentation

+0

私はあなたのコードを使用しました、これは動作しています。してくださいあなたはより明確に説明する、あなたがしたこと正確に、私は理解できません..ありがとう –

+0

フレームを設定しましたか? –

+2

私の回答が更新され、あなたに説明しようとしました。詳細についてはリンクで確認できます – Konstantin

0

フレームが(0, 0, 320, 120)のUIViewを設定しています。このフレームはiPhone 4の画面に適合します。電話の画面の幅は320ピクセルです。しかし、あなたはiPhone 6/6sでコードを実行すると同じことは期待できません。自動サイズ設定はこれを処理しません。そのためには、制約/ autolayoutを使用する必要があります。

Autoresizing masks describe how a subview will resize or move when its superview is resized.

あなたは携帯電話の向きを変更するのであれば、このビューを追加した後、これを適宜の位置にビューのサイズを変更します。しかし、最初にスーパービューに従ってフレームを設定する必要があります。幅を動的に設定することができます:(0, 0, self.view.frame.size.width, 120)

関連する問題