1

superview.frame = CGRectMake(0,0,200,200);とがある場合subview.frame = CGRectMake(0,0,100,100)、サブビューの幅と高さは柔軟です。スーパービューのフレームが に変わると(0,0,150,150)、サブビューのフレームが に変わるはず(0, 0, 75, 75)です。しかし、そうではありません。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIView * view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view1.backgroundColor = [UIColor blackColor];
[self.view addSubview:view1];

UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.backgroundColor = [UIColor redColor];
view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[view1 addSubview:view2];

view1.frame = CGRectMake(0, 0, 150, 150);
[self printRect:view2.frame];
}

- (void) printRect:(CGRect)rect
{
NSLog(@"%0.1f, %0.1f, %0.1f, %0.1f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
}

出力:

0.0, 0.0, 50.0, 50.0

スーパービューのフレームが (0,0,100,100) に変わると、サブビューのフレームは (0,0,0,0) になります。

どうして..

4

2 に答える 2

2

あなたが忘れていたので、あなたのコード行を私のものに置き換えてUIViewAutoresizingFlexibleBottomMarginくださいUIViewAutoresizingFlexibleTopMargin

  view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
于 2012-10-30T10:18:23.427 に答える
0

あなたのコードでview2は、上から時計回りに(0、100、100、0)である固定マージンがあります。これは、あなたの自動サイズ変更動作を説明していますview2。拡大しただけでは、view2高さと幅は変わりませんview1。そのため、の autoresziing マスクにUIViewAutoresizingFlexibleBottomMarginandUIViewAutoresizingFlexibleTopMarginを追加する必要がありますview2

于 2012-10-30T10:31:58.323 に答える