我有UIViewController
确实在其UISearchBar
方法中添加UITableView
和-viewDidLoad
作为其view
属性的子视图。添加后,我使用可视化格式语言创建约束:
NSDictionary *views = @{@"searchBar": [_searchController searchBar],
@"dataTable": [self tableView]};
NSArray *horizontalSearchBarConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|"
options:0
metrics:nil
views:views];
NSArray *horizontalDataTableConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[dataTable]|"
options:0
metrics:nil
views:views];
NSArray *verticalSearchBarDataTableConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[searchBar(44)][dataTable]|"
options:0
metrics:nil
views:views];
NSMutableArray *allConstraints = [NSMutableArray new];
[allConstraints addObjectsFromArray:horizontalDataTableConstraints];
[allConstraints addObjectsFromArray:horizontalSearchBarConstraints];
[allConstraints addObjectsFromArray:verticalSearchBarDataTableConstraints];
[[self view] addConstraints:allConstraints];
阅读:宽度应该是整个屏幕的宽度,searchBar
的高度应该是44,dataTable
的高度应该从searchBar
的底部伸出在视图的底部。
在此之前,我将这些观看次数translatesAutoresizingMaskIntoConstraints
设置为NO
。 UIViewController
本身以模态视图显示。我们正在谈论iOS 6。
我得到的:一个空视图。只是纯白色。
我错过了什么?我仍然是Auto Layout的新手,并试着绕过它。
谢谢!
更新:添加整个-viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
BOOL translatesAutoreszingMaskIntoConstraints = NO;
[[self view] setTranslatesAutoresizingMaskIntoConstraints:translatesAutoreszingMaskIntoConstraints];
// manually create the search bar
UISearchBar *searchBar = [[UISearchBar alloc] init];
[searchBar setDelegate:self];
[searchBar setShowsCancelButton:NO];
[searchBar setTranslatesAutoresizingMaskIntoConstraints:translatesAutoreszingMaskIntoConstraints];
// create the search bar controller
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[_searchController setDelegate:self];
[[_searchController searchResultsTableView] setDelegate:self];
[_searchController setSearchResultsDataSource:self];
[_searchController setSearchResultsDelegate:self];
// add the search bar
[[self view] addSubview:[_searchController searchBar]];
// manually create the table view and add it to the view
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
[_tableView setBackgroundView:nil];
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];
[_tableView setSeparatorColor:NEA_COLOR_MEDIUM_GREY];
[_tableView setBackgroundColor:NEA_COLOR_VIEW_BACKGROUND];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[_tableView setTranslatesAutoresizingMaskIntoConstraints:translatesAutoreszingMaskIntoConstraints];
[[self view] addSubview:[self tableView]];
// set up the layout using Auto Layout
NSDictionary *views = @{@"searchBar": [_searchController searchBar],
@"dataTable": [self tableView]};
NSArray *horizontalSearchBarConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|"
options:0
metrics:nil
views:views];
NSArray *horizontalDataTableConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[dataTable]|"
options:0
metrics:nil
views:views];
NSArray *verticalSearchBarDataTableConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[searchBar(44)][dataTable]|"
options:0
metrics:nil
views:views];
NSMutableArray *allConstraints = [NSMutableArray new];
[allConstraints addObjectsFromArray:horizontalDataTableConstraints];
[allConstraints addObjectsFromArray:horizontalSearchBarConstraints];
[allConstraints addObjectsFromArray:verticalSearchBarDataTableConstraints];
[[self view] addConstraints:allConstraints];
// add our little gradient
UIImage *shadow = [UIImage imageNamed:@"searchbar_bg_shadow"];
UIImageView *shadowView = [[UIImageView alloc] initWithImage:shadow];
[shadowView setFrame:CGRectMake(0, 44, 900, 14)];
[shadowView setBackgroundColor:[UIColor clearColor]];
[shadowView setTranslatesAutoresizingMaskIntoConstraints:YES];
[[self view] addSubview:shadowView];
// add the cancel button for the search bar
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ui_navigationbar_close"] style:UIBarButtonItemStyleBordered target:self action:@selector(cancel:)];
[[self navigationItem] setRightBarButtonItem:cancelButton];
// set a short back button title
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
}
更新:附加信息
正在通过故事板创建封装UINavigationController
。在那里,我的班级UIViewController
添加了rootViewController
。{{1}}。多数民众赞成。
当我在该格式字符串中手动添加宽度时,它会显示视图。因此,当我手动指定尺寸时,它可以工作。但是,我猜,我可以简单地使用框架......
答案 0 :(得分:2)
我不知道您的问题是否已解决,但我发现您的代码存在潜在问题。
尝试删除以下代码行:
[[self view] setTranslatesAutoresizingMaskIntoConstraints:translatesAutoreszingMaskIntoConstraints];
不要为根视图将该属性设置为NO。