当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS开发从入门到精通--自动布局子视图

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

2019年01月09日  | 移动技术网移动技术  | 我要评论

这里写图片描述

#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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网