如何根据父视图大小变化改变子视图大小变化(autoresizingMask)

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *v=[[UIView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
    v.backgroundColor=[UIColor redColor];
    v.tag=100;
    [self.view addSubview:v];
    UIView *v1=[[UIView alloc]initWithFrame:CGRectMake(20, 20, 160, 160)];
    v1.backgroundColor=[UIColor blueColor];
    v1.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    [v addSubview:v1];
    UIView *v2=[[UIView alloc]initWithFrame:CGRectMake(20, 20, 120, 120)];
    v2.backgroundColor=[UIColor yellowColor];
    v2.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    [v1 addSubview:v2];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:@"缩小" forState:UIControlStateNormal];
    btn.frame=CGRectMake(50, 400, 70, 40);
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn2 setTitle:@"放大" forState:UIControlStateNormal];
    btn2.frame=CGRectMake(250, 400, 70, 40);
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(suoxiao) forControlEvents:UIControlEventTouchUpInside];
}

-(void)doSomething{
    UIView *v=[self.view viewWithTag:100];
//    v.center=CGPointMake(200, 300);
    v.frame=CGRectMake(100, 200, 150, 150);
}
-(void)suoxiao{
    UIView *v=[self.view viewWithTag:100];
//    v.center=CGPointMake(50, 50);
    v.frame=CGRectMake(50, 50, 200, 200);
}

 

posted on 2013-12-20 11:05  IOS菜菜菜菜鸟  阅读(1045)  评论(0编辑  收藏  举报

导航