1

是否可以更改 iphone 联系人应用程序类型的屏幕以使搜索栏始终保持在顶部?如果是的话怎么办?

4

2 回答 2

2

我找到了方法。

这里是

  1. 将表格视图拉到单独的视图中
  2. 首先放置搜索栏,然后在新的单独视图中放置表格视图
  3. 为表视图创建一个 iboutlet 并连接它。
  4. 对 tableview 委托进行适当的更改。
  5. 更改添加到新 tableview 的 uitableview 的测量值。
于 2010-10-13T14:26:05.550 回答
0

我知道这是一个老问题,但我找到了一个解决方案,它适用于经典的 UITableViewController 和 UTSearchDisplayController。

我为 searchBar 1st 创建了一个容器视图,然后将搜索栏放入其中。容器不得裁剪到边界。在此之后,您可以更改搜索栏相对于容器的位置。这样做的一个问题是,搜索栏无法处理用户交互。所以我们需要使用我们自己的容器来获取低于其真实框架的事件。

我们的容器类:

@interface _SearchContainerView : UIView
@end

@implementation _SearchContainerView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.subviews.count > 0) {
        UISearchBar *searchBar = (UISearchBar *) self.subviews[0];
        CGRect f = searchBar.frame;
        f = CGRectMake(0, 0, f.size.width, f.origin.y + f.size.height);
        if (CGRectContainsPoint(f, point)) return YES;
    }
    return [super pointInside:point withEvent:event];
}
@end

如果您以编程方式创建 searchBar,则可以使用以下类似代码进行设置:

- (void)setSearchEnabled:(BOOL)searchEnabled {
    if (searchBar == nil && searchEnabled) {
        searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)];
        searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                                                contentsController:self];
        searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
                                     | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        searchDisplayController.delegate = self;
        searchDisplayController.searchResultsDataSource = self;

        searchContainer = [[_SearchContainerView alloc] initWithFrame:searchBar.frame];
        [container addSubview:searchBar];
        container.clipsToBounds = NO;

        self.tableView.tableHeaderView = container;

    }  else {
        [searchBar removeFromSuperview];
        self.tableView.tableHeaderView = nil;
        searchBar = nil;
        searchDisplayController = nil;
        searchContainer = nil;
    }
}

然后可以根据tableView的滚动位置来改变位置:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (searchBar == nil || searchDisplayController.isActive) return;
    CGRect b = self.tableView.bounds;
    // Position the searchbar to the top of the tableview
    searchBar.frame = CGRectMake(0, b.origin.y, b.size.width, 44);
}

最后一部分是搜索后恢复所有内容:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    // Restore header alpha
    searchContainer.alpha = 1.0;
    // Place the searchbar back to the tableview
    [searchBar removeFromSuperview];
    [searchContainer addSubview:searchBar];
    // Refresh position and redraw
    CGPoint co = self.tableView.contentOffset;
    [self.tableView setContentOffset:CGPointZero animated:NO];
    [self.tableView setContentOffset:co animated:NO];
}
于 2015-01-05T16:58:04.083 回答