Using AutoLayout programmatically for search bar and UITableView

前端 未结 3 1209
野趣味
野趣味 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条回答
  •  太阳男子
    2021-01-20 06:36

    You can add not only the top layout guide constraint from Interface Builder (Storyboard/xib) but also by programmatically.

    Have you ever try KVConstraintExtensionsMaster library to apply constraints that I have implemented. using this library you can add the top and bottom layout guide constraints Programmatically and many more that are not possible or very difficult from storyboard or xib.

    Put the below code in the viewDidLoad is:

        // create containerView
        containerView = [UIView prepareNewViewForAutoLayout];
        [containerView setBackgroundColor:[UIColor brownColor]];
        [self.view addSubview:containerView];
    
        // create Search controller
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        self.searchController.searchResultsUpdater = self;
        self.searchController.dimsBackgroundDuringPresentation = NO;
        self.searchController.searchBar.delegate = self;
        self.searchController.searchBar.scopeButtonTitles = @[@"A", @"B", @"C"];
    
        // create UITableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView setBackgroundColor:[UIColor blueColor]];
    
        [self.tableView prepareViewForAutoLayout];
    
        [containerView addSubview:self.searchController.searchBar];
        [containerView addSubview:self.tableView];
    
        // now applying the constraints by using KVConstraintExtensionsMaster library
        // adding leading and trailing contraints of containerView
        [containerView applyLeadingAndTrailingPinConstraintToSuperviewWithPadding:defualtConstant];
    
        // adding Top and Bottom Layout Guidecontraint of containerView
        [self applyTopLayoutGuideConastrainToView:containerView withPadding:defualtConstant];
        [self applyBottomLayoutGuideConastrainToView:containerView withPadding:defualtConstant];
    
        // adding Top constraint of searchBar
        [self.searchController.searchBar applyTopPinConstraintToSuperviewWithPadding:defualtConstant];
    
        // adding leading and trailing contraints of searchBar
        [self.searchController.searchBar applyLeadingAndTrailingPinConstraintToSuperviewWithPadding:defualtConstant];
    
        // adding leading and trailing contraints of tableView
        [self.tableView applyLeadingAndTrailingPinConstraintToSuperviewWithPadding:defualtConstant];
    
        // adding Bottom constraint of tableView
        [self.tableView applyBottomPinConstraintToSuperviewWithPadding:defualtConstant];
    
        // adding vertical constraint between two Sibling views
        // to increase the vertical space between searchBar & tableView, update the spacing value like 10.0 or what you want  
        [self.searchController.searchBar applyConstraintFromSiblingViewAttribute:NSLayoutAttributeBottom toAttribute:NSLayoutAttributeTop ofView:self.tableView spacing:defualtConstant];
    

提交回复
热议问题