使搜索栏保持在顶部

发布于 2024-09-27 07:06:40 字数 54 浏览 0 评论 0原文

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

Is it possible to change the iphone contact application kind of screen to have the searchbar staying on the top always ? if yes how ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江心雾 2024-10-04 07:06:40

我找到了方法。

这里是

  1. 将表视图拉到一个单独的视图中,
  2. 首先放置搜索栏,然后在新的单独视图中的表视图
  3. 为表视图创建一个 iboutlet 并连接它。
  4. 对表视图委托进行适当的更改。
  5. 更改添加到新表视图的 uitableview 的度量。

I found out the way to do it.

here it is

  1. pull the table view to a separate view
  2. place the search bar first and then the table view in the new separate view
  3. create a iboutlet for table view and connect the same.
  4. make appropriate changes for tableview delegate.
  5. Change the measurments of the uitableview added to the new tableview.
叹倦 2024-10-04 07:06:40

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

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

我们的容器类:

@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];
}

I know it's an old question, but I found out a solution for this, which works with the classic UITableViewController and UTSearchDisplayController.

I created a container view for the searchBar 1st then put the searchbar inside it. The container must not clip to bounds. After this you can change the position of the searchbar relative to the container. One problem with this that this way the searchbar not handle user interactions. So we need to use our own container which get the events below its real frame.

Our container class:

@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

If you create the searchBar programmatically you can set the this up with a following like code:

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

Then you can change the position based on the tableView's scroll position:

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

And the last part is to restore everything after searching:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文