IOS(UI)_AutoLayout(自动布局)01

来源:互联网 发布:linux 局域网 编辑:程序博客网 时间:2024/06/03 21:33
- (void)viewDidLoad {    [super viewDidLoad];        UIView *view1=[UIView new];    view1.backgroundColor=[UIColor redColor];    view1.translatesAutoresizingMaskIntoConstraints=NO;    view1.tag=1000;    [self.view addSubview:view1];        NSLayoutConstraint *constraint1=[NSLayoutConstraint constraintWithItem:view1                                                                 attribute:NSLayoutAttributeLeading                                                                 relatedBy:NSLayoutRelationEqual                                                                    toItem:self.view                                                                 attribute:NSLayoutAttributeLeading                                                                multiplier:1                                                                  constant:20];    [self.view addConstraint:constraint1];        NSLayoutConstraint *constraint2=[NSLayoutConstraint constraintWithItem:view1                                                                 attribute:NSLayoutAttributeTop                                                                 relatedBy:NSLayoutRelationEqual                                                                    toItem:self.view                                                                 attribute:NSLayoutAttributeTop                                                                multiplier:1                                                                  constant:20];    [self.view addConstraint:constraint2];        NSLayoutConstraint *constraint3=[NSLayoutConstraint constraintWithItem:view1                                                                 attribute:NSLayoutAttributeTrailing                                                                 relatedBy:NSLayoutRelationEqual                                                                    toItem:self.view                                                                 attribute:NSLayoutAttributeTrailing                                                                multiplier:1                                                                  constant:-20];    [self.view addConstraint:constraint3];        NSLayoutConstraint *constraint4=[NSLayoutConstraint constraintWithItem:view1                                                                 attribute:NSLayoutAttributeHeight                                                                 relatedBy:NSLayoutRelationEqual                                                                    toItem:self.view                                                                 attribute:NSLayoutAttributeHeight                                                                multiplier:0.5                                                                  constant:0];    [self.view addConstraint:constraint4];            }

横屏时:



竖屏时:



如何判断横竖屏:

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        UIView *view1=[UIView new];    view1.backgroundColor=[UIColor redColor];    view1.translatesAutoresizingMaskIntoConstraints=NO;    view1.tag=1000;    [self.view addSubview:view1];        NSLayoutConstraint *constraint1=[NSLayoutConstraint constraintWithItem:view1                                                                 attribute:NSLayoutAttributeLeading                                                                 relatedBy:NSLayoutRelationEqual                                                                    toItem:self.view                                                                 attribute:NSLayoutAttributeLeading                                                                multiplier:1                                                                  constant:20];    [self.view addConstraint:constraint1];        NSLayoutConstraint *constraint2=[NSLayoutConstraint constraintWithItem:view1                                                                 attribute:NSLayoutAttributeTop                                                                 relatedBy:NSLayoutRelationEqual                                                                    toItem:self.view                                                                 attribute:NSLayoutAttributeTop                                                                multiplier:1                                                                  constant:20];    [self.view addConstraint:constraint2];        NSLayoutConstraint *constraint3=[NSLayoutConstraint constraintWithItem:view1                                                                 attribute:NSLayoutAttributeTrailing                                                                 relatedBy:NSLayoutRelationEqual                                                                    toItem:self.view                                                                 attribute:NSLayoutAttributeTrailing                                                                multiplier:1                                                                  constant:-20];    [self.view addConstraint:constraint3];        NSLayoutConstraint *constraint4=[NSLayoutConstraint constraintWithItem:view1                                                                 attribute:NSLayoutAttributeHeight                                                                 relatedBy:NSLayoutRelationEqual                                                                    toItem:self.view                                                                 attribute:NSLayoutAttributeHeight                                                                multiplier:0.5                                                                  constant:0];    [self.view addConstraint:constraint4];            }//检测横竖屏//是否支持横竖-(BOOL)shouldAutorotate{    return YES;//支持横屏}//支持几种朝向-(UIInterfaceOrientationMask)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskAllButUpsideDown;//home键向上}-(void)viewWillLayoutSubviews{    [super viewWillLayoutSubviews];        UIView *view=(UIView *)[self.view viewWithTag:1000];   <p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:10px;">    switch ([UIDevice currentDevice].orientation)</span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:10px;">    {</span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:10px;">        case UIDeviceOrientationPortrait:</span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:10px;">        {</span></p>             view.backgroundColor=[UIColor greenColor];             [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];                    view.layer.masksToBounds = YES;<span style="white-space:pre"></span><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:12px;">              </span><span style="font-size:10px;">NSLog(@"<span style="line-height: normal; font-family: 'Heiti SC Light';">竖屏</span>");</span></p>        }            break;        case UIDeviceOrientationLandscapeLeft:            case UIDeviceOrientationLandscapeRight:        {            view.backgroundColor=[UIColor greenColor];            NSLog(@"横屏");        }            break;        default:            break;    }}

ipone横屏和竖屏效果

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskAllButUpsideDown;//home键向上}-(void)viewWillLayoutSubviews{    [super viewWillLayoutSubviews];        //UIView *view=(UIView *)[self.view viewWithTag:1000];    switch ([UIDevice currentDevice].orientation)    {        case UIDeviceOrientationPortrait:        {            //view.backgroundColor=[UIColor greenColor];             [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];            for(int i=0;i<3;i++)            {                for (int j=0; j<4; j++)                {                    UIView *view1=[UIView new];                    view1.backgroundColor=[UIColor greenColor];                    view1.translatesAutoresizingMaskIntoConstraints=NO;                    //view.layer.masksToBounds = YES;                    view1.layer.cornerRadius=10;                    [self.view addSubview:view1];                                        NSLayoutConstraint *constraint1=[NSLayoutConstraint constraintWithItem:view1                                                                                 attribute:NSLayoutAttributeTop                                                                                 relatedBy:NSLayoutRelationEqual                                                                                    toItem:self.view                                                                                 attribute:NSLayoutAttributeTop                                                                                multiplier:1                                                                                  constant:i*44+(i+1)*20];                    [self.view addConstraint:constraint1];                                        NSLayoutConstraint *constraint2=[NSLayoutConstraint constraintWithItem:view1                                                                                 attribute:NSLayoutAttributeLeft                                                                                 relatedBy:NSLayoutRelationEqual                                                                                    toItem:self.view                                                                                 attribute:NSLayoutAttributeLeft                                                                                multiplier:1                                                                                  constant:j*44+(j+1)*(([UIScreen mainScreen].bounds.size.width)-44*4)/5];                    [self.view addConstraint:constraint2];                                                            NSLayoutConstraint *constraint3=[NSLayoutConstraint constraintWithItem:view1                                                                                 attribute:NSLayoutAttributeWidth                                                                                 relatedBy:NSLayoutRelationEqual                                                                                    toItem:nil                                                                                 attribute:NSLayoutAttributeWidth                                                                                multiplier:1                                                                                  constant:44];                    [self.view addConstraint:constraint3];                                        NSLayoutConstraint *constraint4=[NSLayoutConstraint constraintWithItem:view1                                                                                 attribute:NSLayoutAttributeHeight                                                                                 relatedBy:NSLayoutRelationEqual                                                                                    toItem:nil                                                                                 attribute:NSLayoutAttributeHeight                                                                                multiplier:1                                                                                  constant:44];                    [self.view addConstraint:constraint4];                }            }                        NSLog(@"竖屏");        }            break;        case UIDeviceOrientationLandscapeLeft:            case UIDeviceOrientationLandscapeRight:        {            //view.backgroundColor=[UIColor greenColor];            [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];            int count=0;            for(int i=0;i<2;i++)            {                for (int j=0; j<8; j++)                {                    count++;                    UIView *view2=[UIView new];                    view2.backgroundColor=[UIColor redColor];                    view2.layer.cornerRadius=10;                    view2.translatesAutoresizingMaskIntoConstraints=NO;                    [self.view addSubview:view2];                    if (count<=12)                    {                        NSLayoutConstraint *constraint1=[NSLayoutConstraint constraintWithItem:view2                                                                                     attribute:NSLayoutAttributeTop                                                                                     relatedBy:NSLayoutRelationEqual                                                                                        toItem:self.view                                                                                     attribute:NSLayoutAttributeTop                                                                                    multiplier:1                                                                                      constant:i*44+(i+1)*20];                        [self.view addConstraint:constraint1];                                                NSLayoutConstraint *constraint2=[NSLayoutConstraint constraintWithItem:view2                                                                                     attribute:NSLayoutAttributeLeft                                                                                     relatedBy:NSLayoutRelationEqual                                                                                        toItem:self.view                                                                                     attribute:NSLayoutAttributeLeft                                                                                    multiplier:1                                                                                      constant:j*44+(j+1)*(([UIScreen mainScreen].bounds.size.width)-44*8)/9];                        [self.view addConstraint:constraint2];                                                NSLayoutConstraint *constraint3=[NSLayoutConstraint constraintWithItem:view2                                                                                     attribute:NSLayoutAttributeWidth                                                                                     relatedBy:NSLayoutRelationEqual                                                                                        toItem:nil                                                                                     attribute:NSLayoutAttributeWidth                                                                                    multiplier:1                                                                                      constant:44];                        [self.view addConstraint:constraint3];                                                NSLayoutConstraint *constraint4=[NSLayoutConstraint constraintWithItem:view2                                                                                     attribute:NSLayoutAttributeHeight                                                                                     relatedBy:NSLayoutRelationEqual                                                                                        toItem:nil                                                                                     attribute:NSLayoutAttributeHeight                                                                                    multiplier:1                                                                                      constant:44];                        [self.view addConstraint:constraint4];                    }                }            }            NSLog(@"横屏");        }            break;        default:            break;    }}

竖屏


i*44+(i+1)*20

间距欢度为20

当循环为0时

0+20只有间隔

当循环为1时

44+40可以理解为20+44+20

有循环为2时

2*44+3*20

理解两个高度为44的正方形,3个间隔


横屏


横向的的运行原理了和竖向时差不多的

先的到屏幕宽度在除上有多少个圆角正方形得到总得间隔 在出上圆角正方形的的数目加1(为什么加一呢因为间隔数是比圆角正方形多一个)得到的就是它们之间的间隔大小

通过2*8循环输出因为上面绿色的圆角正方形只有12所有当2*8循环到12时就不往下处理可以通过break结束循环(定义一个变量来累加直到判断为12时)





0 0
原创粉丝点击