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
Aber wenn ich 6 gleichen Code iPhone betreibe ich die verschiedenen Ausgabestelle zu bekommen. Unten ist der Screenshot.
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.
Ü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
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. –