Reputation: 8833
I'm programming for IOS objective c for a long time now, and still at situations like so, it feels insane for me to start calculating distances each time I'm performing a simple UI action as so:
Say I have a page with a few labels:
NAME : MATAN
MUSIC : TECHNO
AGE : 26
Now in some specific cases, I would like to insert one more label between "MUSIC" and "AGE".
Normally I would check the height of the new label, and move the "AGE" label down by code.
This is CRAZY that I didn't find yet a better way than this, because when dealing with more complex views, this becomes HARD!
Any suggestions ?
Upvotes: 1
Views: 803
Reputation: 833
UITableView for this use is kind of overkill given the amount of boilerplate code you have to add. A better solution is to use two overlapping container view controllers, each contains the proper fixed layout of the required labels. You can programmatically enable and disable the two containers respectively. See Apple guide below for some examples:
Upvotes: 1
Reputation: 877
Somone mentioned using constraints. This should get you pointed in the right direction for that kind of thing
self.automaticallyAdjustsScrollViewInsets = NO;
UILabel *label1 = [[UILabel alloc] init];
UILabel *label2 = [[UILabel alloc] init];
label1.text = @"NAME : MATAN";
label2.text = @"AGE : 26";
[self.view addSubview:label1];
[self.view addSubview:label2];
[label1 setTranslatesAutoresizingMaskIntoConstraints: NO];
[label2 setTranslatesAutoresizingMaskIntoConstraints: NO];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(label1, label2);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[label1]-20-|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[label2]-20-|" options:0 metrics: 0 views:viewsDictionary]];
NSArray * vertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1]-10-[label2]" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:vertConstraint];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
UILabel *label3 = [[UILabel alloc] init];
label3.text = @"MUSIC : TECHNO";
[label3 setTranslatesAutoresizingMaskIntoConstraints: NO];
[self.view addSubview:label3];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(label1, label2, label3);
[self.view removeConstraints:vertConstraint];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[label3]|" options:0 metrics: 0 views:viewsDictionary]];
NSArray * newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1]-10-[label3]-10-[label2]" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:newVertConstraint];
});
Upvotes: 0
Reputation: 284
You could always use tableViews to make it more easy.
If not then read something about Constraints. Maybe you already know about it. But they are a big help if you can do it properly.
Read more about constraints here
Upvotes: 0
Reputation: 4728
I suggest you put it all in a UITableView, which is designed for exactly this kind of stuff, and has mechanism to insert/remove cells..
Upvotes: 2