在iOS中按相对位置布局视图的最佳方法?

问题描述

| 我对iOS中的用户界面有疑问,尤其是在iPad上。 我的应用程序显示搜索结果中的餐厅信息。并非所有字段都是必需的,因此某些字段为空。它们可以是电话号码,等级,菜单等。 因此,基本上我想做的是以布局格式显示诸如
UILabel
和ѭ1views之类的视图,但是我不想显示任何带有空字符串的视图。因此,视图之间不应有任何空白空间。 我的操作方式是,如果字段不为空,则启动其视图,然后通过跟踪应显示的当前高度来显示在先前显示的视图下方。 尽管这可行,但我认为它的工作方式似乎很乏味。是否存在显示具有相对位置的视图的最佳实践? 谢谢 :)     

解决方法

一种方法是使用UITableView并遵循此答案中的说明: 根据Objective-C中的内容自动调整UITableViewCell的高度     ,我已经创建了一个库来解决此问题:CSLinearLayoutView 您可以这样使用它:
// 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
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...