ios7以上设置UIView的相对布局,用NSLayoutConstraint

self.label = [[UILabel alloc] init];//这里就不用setframe 了,,,因为后边的layoutconstraint会帮你自适应

    self.label.text = @"加载中...";

    self.label.textColor = [UIColor whiteColor];

    self.label.backgroundColor = [UIColor clearColor];

    

    [[self view] setBackgroundColor:[UIColor blackColor]];

    

    [self.loadingAni startAnimating];

    [self.view addSubview:self.label];

    

    [self.label setTranslatesAutoresizingMaskIntoConstraints:NO];//一定要加上这句

    

    pConstraint = [NSLayoutConstraint constraintWithItem:self.label //约束目标

                                               attribute:NSLayoutAttributeTop//约束的属性,有top,bottom,left,right,centerX,centerY等

                                               relatedBy:NSLayoutRelationEqual //一般用等于

                                                  toItem:self.loadingAni //参照对象,

                                               attribute:NSLayoutAttributeBottom // 参照属性

                                              multiplier:1.0 //倍率,

                                                constant:30.0/3]; //偏移量,偏移量可正可负,右,下为正,左上为负,除以3表示在6plus 上的偏移为30,但在4上面是10,即分辨率缩放

    [self.view addConstraint:pConstraint];

    pConstraint = [NSLayoutConstraint constraintWithItem:self.label

                                               attribute:NSLayoutAttributeCenterX

                                               relatedBy:NSLayoutRelationEqual

                                                  toItem:self.view

                                               attribute:NSLayoutAttributeCenterX

                                              multiplier:1.0

                                                constant:0];

    [self.view addConstraint:pConstraint];

posted @ 2015-02-09 12:59  Mareon  阅读(306)  评论(0编辑  收藏  举报