2016-05-27 10 views
0

Ich habe mehrere Label zu zeigen.So implementieren Sie automatische Layout-Einschränkung für mehrere Etiketten programmatisch

Das Problem ist jedes Etikett, das für verschiedene Arten von Simulator unterschiedlich ist.

Unten ist mein Code, um Label auf Xib-Dateien anzuzeigen.

- (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] 
} 

Ich bin auch die my Xib-Dateien anhängen. Unten sind die Screenshots

Jetzt, wenn ich die Punktzahl auf meinem iPhone 4s Simulator laufen die Ausgabe ist völlig in Ordnung. Unten ist der Screenshot

enter image description here

Aber wenn ich 6 gleichen Code iPhone betreibe ich die verschiedenen Ausgabestelle zu bekommen. Unten ist der Screenshot.

enter image description here

Ich habe versucht, Auto-Layout-Beschränkungs implemeting aber nicht den Erfolg bekommen haben. Auch bitte helfen Sie mir, automatische Layout-Einschränkung programmatisch anzuwenden, so dass für jeden Simulator die Position und Position festgelegt ist.

Vielen Dank im Voraus.

+0

Überprüfen Sie eine schöne automatischen Layout-Bibliothek https://github.com/SnapKit/Masonry, Es behandelt Constraints auf eine verständlichere Art und Weise – azimov

+0

Dies ist eine gute Anwendung für 'UIStackView's. Dies ist eine viel einfachere Option, als Zeit damit zu verschwenden, neue Bibliotheken zu lernen und unnötigen zusätzlichen Code zu importieren. –

Antwort

1

Versuchen Sie, diese sehr einfache Art und Weise: -

ViewController.h Code ist hier:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UIView *bigBlueView; 
@property (strong, nonatomic) IBOutlet UILabel *caption; 
@property (strong, nonatomic) IBOutlet UIButton *buttonOne; 
@property (strong, nonatomic) IBOutlet UIButton *buttonTwo; 
@property (strong, nonatomic) IBOutlet UILabel *lblOutlet; 

@end 

ViewController.m Code ist hier:

#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 

Ausgabe Bildschirm Porträt sieht l diese ike: -

enter image description here

Ausgabebild Landschaft sieht wie folgt aus: -

enter image description here

Verwandte Themen