是否可以更改iPhone联系应用程序的屏幕类型,以使搜索栏始终保持顶部?如果是的话?

有帮助吗?

解决方案

我发现了这样做的道路。

这里是

  1. 将表视图拉到单独的视图
  2. 首先将搜索栏放置在新的单独视图中
  3. 创建一个用于表视图的IBOUTLET并连接相同。
  4. 对桌面委托进行适当的更改。
  5. 更改添加到新的TableView中的UITATIONVIEW的测量值。

其他提示

我知道这是一个古老的问题,但是我找到了一个解决方案,它可以与Classic uitaiteViewController和utsearchDisplayController一起使用。

我为搜索栏创建了一个容器视图,然后将搜索栏放在其中。容器不得夹紧到边界。之后,您可以更改搜索栏相对于容器的位置。一个问题是,这样的方式搜索栏无法处理用户交互。因此,我们需要使用自己的容器,使事件在其真实框架下方。

我们的集装箱类:

@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

如果以编程方式创建搜索栏,则可以使用以下类似代码设置此搜索栏:

- (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;
    }
}

然后,您可以根据桌面的滚动位置更改位置:

-(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];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top