I'm trying to move a view down programmatically given a certain number of rows using this code:
view.frame = CGRectMake(0, 40*rows, 320, 80)
But it will only move down to a certain point. I think the problem is that the parent view doesn't take up enough space for the view to move into, but I'm not sure. Here's a screenshot of what I'm talking about:
I'm programmatically adding links to the "Links" section and want to move the "Settings" section down to account for this. How can I accomplish this?
Don't try to calculate positions like this! I mean, you can, but you're asking for trouble. Think about all the trouble you'll run into if you ever want to support landscape mode, or if Apple decides to release another screen size. You'd need to make these calculations all over again.
2 solutions:
TableViews already have dynamic resizing built-in. I recommend using section headers for "Links", "Settings", "Night Mode". You can use custom Views for section headers by implementing this TableView delegate method:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
The links section will have as many tableViewCells as you need, but Settings and Night Mode will have 0 rows. For this, you can implement:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Essentially you use constraints to place the first link row directly below the "links" view, place each link row directly below the previous link row, and place the "Settings" view directly below the last link row.
(each |
is a constraint that specifies vertical distance between the views.)
[Links]
|
[link row 1]
|
[link row 2]
|
[link row 3]
|
[Settings]
While this method is better than using frames because you can easily support multiple screen sizes, I do not recommend this method. This approach is essentially re-doing a lot of the work UITableView already does for you. But its good to be aware of multiple approaches so you can make an informed decision.