欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发从入门到精通--自动布局子视图

程序员文章站 2023-01-13 14:50:53
#import @interface viewcontroller : uiviewcontroller{ //创建父视图对象 uiview * _superv...

iOS开发从入门到精通--自动布局子视图

#import 

@interface viewcontroller : uiviewcontroller{

    //创建父视图对象
    uiview * _superview;

    //左上角label
    uilabel * _label01;
    //右上角label
    uilabel * _label02;
    //右下角label
    uilabel * _label03;
    //左下角label
    uilabel * _label04;
    //中间
    uiview * _viewcenter;
}


@end
#import "viewcontroller.h"

@interface viewcontroller ()

@end

@implementation viewcontroller

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

    _superview =[[uiview alloc]init];
    _superview.frame = cgrectmake(20, 20, 180, 280);
    _superview.backgroundcolor=[uicolor bluecolor];

    //左上角
    _label01 =[[uilabel alloc]init];
    //位置相对于父亲视图
    _label01.frame=cgrectmake(0, 0, 40, 40);
    _label01.text=@"1";
    _label01.backgroundcolor=[uicolor orangecolor];

    //右上角
    _label02=[[uilabel alloc]init];
    _label02.frame=cgrectmake(180-40, 0, 40, 40);
    _label02.text=@"2";
    _label02.backgroundcolor=[uicolor orangecolor];

    //右下角
    _label03=[[uilabel alloc]init];
    _label03.frame=cgrectmake(180-40, 280-40, 40, 40);
    _label03.text=@"3";
    _label03.backgroundcolor=[uicolor orangecolor];

    //左下角
    _label04=[[uilabel alloc]init];
    _label04.frame=cgrectmake(0, 280-40, 40, 40);
    _label04.text=@"4";
    _label04.backgroundcolor=[uicolor orangecolor];

    [_superview addsubview:_label01];
    [_superview addsubview:_label02];
    [_superview addsubview:_label03];
    [_superview addsubview:_label04];

    //中间
    _viewcenter =[[uiview alloc]init];
    _viewcenter.frame=cgrectmake(0, 0, _superview.frame.size.width, 40);
    _viewcenter.center = cgpointmake(180/2, 280/2);
    _viewcenter.backgroundcolor =[uicolor graycolor];
    [_superview addsubview:_viewcenter];

    [self.view addsubview:_superview];

    //自动布局属性设置,通过此变量来调整视图在父亲视图中的位置和大小
    _viewcenter.autoresizingmask =uiviewautoresizingflexibletopmargin | uiviewautoresizingflexiblebottommargin|uiviewautoresizingflexiblewidth|
    uiviewautoresizingflexiblerightmargin |uiviewautoresizingflexibleleftmargin;

    //视图距离父视图的左侧可以变化
    _label02.autoresizingmask = uiviewautoresizingflexibleleftmargin;

    _label03.autoresizingmask = uiviewautoresizingflexibletopmargin |
    uiviewautoresizingflexibleleftmargin ;

    _label04.autoresizingmask = uiviewautoresizingflexibletopmargin;

}


-(void) touchesbegan:(nsset *)touches withevent:(uievent *)event{


    static bool islarge = no;
    [uiview beginanimations:nil context:nil];
    [uiview setanimationduration:1];

    if(islarge){
        _superview.frame=cgrectmake(20, 20, 180, 280);
        islarge=no;
    }else{
        _superview.frame=cgrectmake(10, 10, 300, 480);
        islarge=yes;
    }

    [uiview commitanimations];

}

- (void)didreceivememorywarning {
    [super didreceivememorywarning];
    // dispose of any resources that can be recreated.
}

@end