如何以编程方式为多个标签实现自动布局约束

时间:2016-05-27 10:47:42

标签: ios objective-c

我有多个标签可供展示。

问题是每个标签beg =对于不同类型的模拟器有不同的。

下面是我在xib文件上显示标签的代码。

- (void)viewDidLoad {
[super viewDidLoad];
[self intillizeview];


// Do any additional setup after loading the view from its nib.
}

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

-(void)intillizeview{

int height =0;


for(int i=0; i<10; i++){


    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.tag=i;
    button.frame = CGRectMake(5, 80 + height, 25, 30);
    [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal ];

    [button.titleLabel setFont:[button.titleLabel.font fontWithSize:7]];

    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    //button.backgroundColor= [UIColor blueColor];
    height+= 20;
    [self.view addSubview:button];


}
height=0;
for(int i=0; i<10; i++){



     UILabel  * label = [[UILabel alloc] initWithFrame:CGRectMake (35, 80+height,100, 30)];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text= [NSString stringWithFormat:@"NAME %d", i];
    [label setFont: [label.font fontWithSize:7]];
    height+= 20;
    [self.view addSubview:label];
      }
height=0;

for(int i=0; i<10; i++){


    UILabel  * label = [[UILabel alloc] initWithFrame:CGRectMake(140, 80+height,65, 30)];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text= [NSString stringWithFormat:@"TASK %d", i];
    [label setFont: [label.font fontWithSize:7]];
    height+= 20;
    [self.view addSubview:label];

}
height=0;
for(int i=0; i<10; i++){


    UILabel  * label = [[UILabel alloc] initWithFrame:CGRectMake(210, 80+height,70, 30)];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text= [NSString stringWithFormat:@"Status %d", i];
    [label setFont: [label.font fontWithSize:7]];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(275, 80 + height, 40, 30);
    [button setTitle:@"Sign Off" forState:UIControlStateNormal];
    [button.titleLabel setFont:[button.titleLabel.font fontWithSize:7]];
            button.titleLabel.textAlignment = NSTextAlignmentCenter;

    height+= 20;
    [self.view addSubview:label];


    [self.view addSubview:button];

}


[enter image description here][1]
}

我还附加了我的xib文件。下面是截屏

现在,当我在iPhone 4s模拟器上运行得分时,输出非常好。以下是截图

enter image description here

但是,当我运行相同的代码iPhone 6时,我得到不同的输出位置。下面是截图。

enter image description here

我尝试过实施自动布局约束,但没有取得成功。另外请帮我以编程方式应用自动布局约束,以便对于任何模拟器,位置和位置都是固定的。

提前致谢。

1 个答案:

答案 0 :(得分:1)

尝试这种非常简单的方式: -

ViewController.h代码在这里:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDictionary *viewsDictionary = @{@"superView":self.view,
                                      @"bigBlueView":self.bigBlueView,
                                      @"caption":self.caption,
                                      @"buttonOne":self.buttonOne,
                                      @"buttonTwo":self.buttonTwo,
                                      @"lblOutlet":self.lblOutlet
                                      };

    [self.view removeConstraints:[self.view constraints]];
    //[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];

    NSArray *bigBluewidthConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[bigBlueView(==300)]" options:0 metrics:nil views:viewsDictionary];
    [self.view addConstraints:bigBluewidthConstraint];

    NSArray *bigBlueHeightConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[bigBlueView(==250)]" options:0 metrics:nil views:viewsDictionary];
    [self.view addConstraints:bigBlueHeightConstraint];

    NSArray *lblOutletTopConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-25-[lblOutlet]" options:0 metrics:nil views:viewsDictionary];
    [self.view addConstraints:lblOutletTopConstraint];
    NSArray *lblOutletCenterConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superView]-(<=1)-[lblOutlet]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:viewsDictionary];
    [self.view addConstraints:lblOutletCenterConstraint];

    NSArray *bigBlueTopConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[bigBlueView]" options:0 metrics:nil views:viewsDictionary];
    [self.view addConstraints:bigBlueTopConstraint];
    NSArray *bigBlueCenterConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superView]-(<=1)-[bigBlueView]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:viewsDictionary];
    [self.view addConstraints:bigBlueCenterConstraint];

    NSArray *captionTopConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[bigBlueView]-25-[caption]" options:0 metrics:nil views:viewsDictionary];
    [self.view addConstraints:captionTopConstraint];
    NSArray *captionCenterConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superView]-(<=1)-[caption]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:viewsDictionary];
    [self.view addConstraints:captionCenterConstraint];

    NSArray *ButtonOneTopConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[bigBlueView]-60-[buttonOne]" options:0 metrics:nil views:viewsDictionary];
    [self.view addConstraints:ButtonOneTopConstraint];
    NSLayoutConstraint *buttonOneLeadingConstraint = [NSLayoutConstraint constraintWithItem:self.buttonOne attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.bigBlueView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:25];
    [self.view addConstraint:buttonOneLeadingConstraint];

    NSArray *ButtonTwoTopConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[bigBlueView]-60-[buttonTwo]" options:0 metrics:nil views:viewsDictionary];
    [self.view addConstraints:ButtonTwoTopConstraint];
    NSLayoutConstraint *buttonTwoTrailingConstraint = [NSLayoutConstraint constraintWithItem:self.buttonTwo attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.bigBlueView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-25];
    [self.view addConstraint:buttonTwoTrailingConstraint];

}

@end

ViewController.m代码在这里:

onSaveInstanceState()

输出屏幕截图如下所示: -

enter image description here

输出屏幕横向如下所示: -

enter image description here