2015-12-04 5 views
0

Я пытаюсь добавить UISearchBar (фиксированное положение!) Поверх UITableView.UISearchBar - проблема с addSubview?

CGRect rect = self.headerView.frame; 
CGRect newRect = CGRectMake(0, 
          rect.origin.y + rect.size.height, 
          rect.size.width, 
          CZP_SEARCHBAR_HEIGHT); 

UIView *view = [[UIView alloc] initWithFrame:newRect]; 
view.backgroundColor = [UIColor whiteColor]; 

Результат (я получил белый прямоугольник на позицию, где я хочу, чтобы мой бар):

enter image description here

Но если я хочу, чтобы добавить подвид на мой взгляд, Searchbar появляются на 1-й ячейке Tableview (ниже мой взгляд!)

[view addSubview:searchBar]; 

enter image description here

ответ

0

Вот один из способов сделать это. Похоже, вы пытаетесь сделать это в коде вместо раскадровки, так что это пример кода. Это похоже на то, что вы делаете это по-своему, я собрал быстрый проект в качестве примера, который использует popover, он не похож на ваш, но он достаточно близко, чтобы вы, где вы пытаетесь идти я думаю.

Во-первых, вот пример кода, это из контроллера представления, который содержит заголовок, панель поиска и таблицу.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // get the desired size for this popover and setup our header height 
    CGSize viewSize  = self.preferredContentSize; // could also be self.view.bounds.size depending on where you're using it 
    CGFloat headerHeight = 44.0; 

    // setup our desired frames 
    CGRect headerFrame   = CGRectMake(0, 0, viewSize.width, headerHeight); 
    CGRect searchContainerFrame = CGRectMake(0, headerHeight, viewSize.width, headerHeight); 

    // for this frame I'm simply centering it, there's better ways to do it but this is an example 
    CGRect searchBarFrame  = CGRectMake(5, 5, searchContainerFrame.size.width - 10, searchContainerFrame.size.height - 10); 

    // set our tableview frame to be positioned below our header and search container frame 
    CGRect tableviewFrame  = CGRectMake(0, headerHeight *2, viewSize.width, viewSize.height - (headerHeight * 2)); 

    // create our header view and set it's background color 
    UIView *headerView = [[UIView alloc] initWithFrame:headerFrame]; 
    headerView.backgroundColor = [UIColor orangeColor]; 

    // create our container view to hold the search bar (not needed really, but if you want it contained in a view here's how) 
    UIView *searchContainer = [[UIView alloc] initWithFrame:searchContainerFrame]; 
    searchContainer.backgroundColor = [UIColor greenColor]; 

    // instantiate our search bar 
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:searchBarFrame]; 

    // add the search bar to the container view 
    [searchContainer addSubview:searchBar]; 

    // create our tableview and position it below our header and search containers 
    UITableView *tableview = [[UITableView alloc] initWithFrame:tableviewFrame]; 
    tableview.backgroundColor = [UIColor blueColor]; 

    [self.view addSubview:headerView]; 
    [self.view addSubview:searchContainer]; 
    [self.view addSubview:tableview]; 
} 

Этого фрагмент кода дает мне поповер с оранжевой головой, зеленый/серой строкой поиска и Tableview под ним.

enter image description here

EDIT: Если вы заинтересованы в поиске через файл проекта, который я использовал, чтобы положить это вместе, вы можете скачать его с GitHub here

Смежные вопросы