子视图的布局(手动、自动)

2017-10-17  本文已影响0人  李琪_59dc

解决的主要问题:使子视图的位置相对于父视图固定

手动布局子视图

//SuperView.h
#import <UIKit/UIKit.h>
@interface SuperView : UIView{
    UIView *_view01;
    UIView *_view02;
    UIView *_view03;
    UIView *_view04;
    UIView *_view05;
}
-(void)createSubView;
@end
//SuperView.m
#import "SuperView.h"
@implementation SuperView
-(void)createSubView{
    _view01 = [[UIView alloc]init];
    _view01.frame = CGRectMake(0, 0, 40, 40);
    _view01.backgroundColor = [UIColor yellowColor];
    
    _view02 = [[UIView alloc]init];
    _view02.frame = CGRectMake(self.bounds.size.width-40, 0, 40, 40);
    _view02.backgroundColor = [UIColor yellowColor];
    
    _view03 = [[UIView alloc]init];
    _view03.frame = CGRectMake(0,self.bounds.size.height-40, 40, 40);
    _view03.backgroundColor = [UIColor yellowColor];
    
    _view04 = [[UIView alloc]init];
    _view04.frame = CGRectMake(self.bounds.size.width-40, self.bounds.size.height-40, 40, 40);
    _view04.backgroundColor = [UIColor yellowColor];
    
    [self addSubview:_view01];
    [self addSubview:_view02];
    [self addSubview:_view03];
    [self addSubview:_view04];
    
    _view05 = [[UIView alloc]init];
    _view05.frame = CGRectMake(0, self.bounds.size.height/2, self.bounds.size.width, 40);
    _view05.backgroundColor = [UIColor yellowColor];
    
    [self addSubview:_view05];
}

关键方法:

//SuperView.m
//实现手动布局 随视图变化大小
-(void)layoutSubviews{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _view01.frame = CGRectMake(0, 0, 40, 40);
    _view02.frame = CGRectMake(self.bounds.size.width-40, 0, 40, 40);
    _view03.frame = CGRectMake(0,self.bounds.size.height-40, 40, 40);
    _view04.frame = CGRectMake(self.bounds.size.width-40, self.bounds.size.height-40, 40, 40);
    
    _view05.frame = CGRectMake(0, self.bounds.size.height/2-20, self.bounds.size.width, 40);
    [UIView commitAnimations];
}
@end
//  ViewController.m
#import "ViewController.h"
#import "SuperView.h"
@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    SuperView *sView = [[SuperView alloc]init];
    sView.frame = CGRectMake(20, 20, 100, 200);
    sView.backgroundColor = [UIColor orangeColor];
    [sView createSubView];
    [self.view addSubview:sView];
    sView.tag = 101;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(240, 480, 80, 40);
    [btn setTitle:@"放大" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pressLarge) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(240, 520, 80, 40);
    [btn2 setTitle:@"缩小" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(pressSmall) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
}
//放大父亲视图
-(void)pressLarge{
    SuperView *sView = [self.view viewWithTag:101];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    sView.frame = CGRectMake(20, 20, 300, 480);
    [UIView commitAnimations];
}
//缩小父亲视图
-(void)pressSmall{
    SuperView *sView = [self.view viewWithTag:101];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    sView.frame = CGRectMake(20, 20, 100, 200);
    [UIView commitAnimations];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

自动布局子视图

//  ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    _view01 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 180, 300)];
    _view01.backgroundColor = [UIColor blueColor];

    //左上角子视图
    _label1 = [[UILabel alloc]init];
    _label1.frame = CGRectMake(0, 0, 40, 40);
    _label1.text = @"1";
    _label1.backgroundColor= [UIColor orangeColor];

    //右上角子视图
    _label2 = [[UILabel alloc]init];
    _label2.frame = CGRectMake(180-40, 0, 40, 40);
    _label2.text = @"2";
    _label2.backgroundColor = [UIColor orangeColor];

    //左下角子视图
    _label3 = [[UILabel alloc]init];
    _label3.frame = CGRectMake(0, 300-40, 40, 40);
    _label3.text = @"3";
    _label3.backgroundColor = [UIColor orangeColor];

    //右下角子视图
    _label4 = [[UILabel alloc]init];
    _label4.frame = CGRectMake(180-40, 300-40, 40, 40);
    _label4.text = @"4";
    _label4.backgroundColor = [UIColor orangeColor];
    
    [_view01 addSubview:_label1];
    [_view01 addSubview:_label2];
    [_view01 addSubview:_label3];
    [_view01 addSubview:_label4];
    [self.view addSubview:_view01];
    
    _viewCenter = [[UIView alloc]init];
    _viewCenter.frame = CGRectMake(0, 0, 180, 40);
    _viewCenter.center = CGPointMake(180/2, 300/2);
    _viewCenter.backgroundColor = [UIColor yellowColor];
    
    [_view01 addSubview:_viewCenter];
    
    //关键属性设置!!!!!
    //自动布局属性设置,通过此变量来调整视图在俯视图中的位置和大小
    _viewCenter.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _label2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    _label3.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    _label4.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    static BOOL isLarge = NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    if(isLarge == NO){
        _view01.frame = CGRectMake(0, 0, 300, 400);
    }
    else{
        _view01.frame = CGRectMake(0, 0, 180, 300);
    }
    [UIView commitAnimations];
    isLarge = !isLarge;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
上一篇 下一篇

猜你喜欢

热点阅读