Using AutoLayout programmatically for search bar and UITableView

前端 未结 3 1235
野趣味
野趣味 2021-01-20 06:00

I have a container view that is nearly the full screen sans the status bar.

I then created a UISearchController and a UITableView. I am using ios 9 and doing things

3条回答
  •  Happy的楠姐
    2021-01-20 06:33

    Well the constraints are definitely wrong

    They should be like this:

    NSDictionary *views = @{@"tableView": self.tableView,@"searchBar": self.searchController.searchBar};
    
    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|" options:0 metrics:nil views:views]];
    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|" options:0 metrics:nil views:views]];
    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[searchBar(44)][tableView]|" options:0 metrics:nil views:views]];
    

    First of all, as you see, you don't need to create a separate dictionary with the views, but you can use one (more simple IMHO)

    Second the constraints are as follows:

    1. @"H:|[searchBar]|" --> The searchbar is pinned to the left & right of the containerView with the system default (8px) margin
    2. @"H:|[tableView]|" --> The tableView is pinned to the left & right of the containerView with the system default (8px) margin
    3. @"V:|[searchBar(44)][tableView]|" -->

      • The searchbar is pinned to the top of the containerView with the default margin and has a fixed height of 44 pixels
      • The tableView top is pinned to the bottom of the searchBar without any margin
      • The bottom of the tableView is pinned to the bottom of the containerView

提交回复
热议问题