开发者

Best way to layout views with relative positions in iOS?

开发者 https://www.devze.com 2023-02-27 04:23 出处:网络
I have a question about user interface in iOS, especially on iPad.开发者_Python百科 My app displays restaurant information from a search result. Not all fields are required, so some fields are empty

I have a question about user interface in iOS, especially on iPad.开发者_Python百科

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 :)


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消