Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to layout views with relative positions in iOS?

I have a question about user interface in iOS, especially on iPad.

My app displays restaurant information from a search result. Not all fields are required, so some fields are empty. They could be phone numbers, ratings, menus, and etc.

So basically what I would like to do is to display views such as UILabel and UIButton in a layout format, but I don't want to display any views with empty string. So there should not be any empty space between views.

The way I do is if a field is not empty, I initiate its view and then display below the previously displayed view by keeping track of the current height a view should be displayed.

Although this works, I believe that the way it works seems tedious. Is there the best practice for displaying views with relative positions?

Thank you :)

like image 445
Ryan Rho Avatar asked Dec 08 '25 23:12

Ryan Rho


2 Answers

I've created a library to solve just this problem: CSLinearLayoutView

You use it like this:

// create the linear layout view
CSLinearLayoutView *linearLayoutView = [[[CSLinearLayoutView alloc] initWithFrame:self.view.bounds] autorelease];
linearLayoutView.orientation = CSLinearLayoutViewOrientationVertical;
[self.view addSubview:linearLayoutView];

// create a layout item for the view you want to display and add it to the layout view
CSLinearLayoutItem *item = [CSLinearLayoutItem layoutItemForView:someView];
item.padding = CSLinearLayoutMakePadding(5.0, 10.0, 5.0, 10.0);
item.horizontalAlignment = CSLinearLayoutItemHorizontalAlignmentCenter;
item.fillMode = CSLinearLayoutItemFillModeNormal;
[linearLayoutView addItem:item];

// add more items
like image 68
user2393462435 Avatar answered Dec 11 '25 14:12

user2393462435


A way to do that is to use UITableView and follow what is said in this answer: Auto adjust the UITableViewCell height depend on its contents in Objective-C

like image 37
gsempe Avatar answered Dec 11 '25 14:12

gsempe