gpt4 book ai didi

ios - 如何在 UIBarButtonItem 中放置 UISearchBar?

转载 作者:行者123 更新时间:2023-11-29 01:45:17 25 4
gpt4 key购买 nike

我在 iOS 8 中使用 UISearchController,当我制作它的 UISearchBar 时,我的表的标题 View 一切正常。但是现在我需要它在我的 UINavigationBar 的左栏按钮中(不能是标题 View ,因为当我放大导航栏时垂直居中)。当我将 UISearchBar 包裹在 UIBarButtonItem 中时,它大于屏幕的宽度。

off screen search bar

我认为这与我的 View Controller 有关,该 View Controller 是从启用了大小类的 Storyboard初始化的,这意味着我的框架直到 viewDidLayoutSubviews 才设置。但是,我在工具栏中的分段控件也遇到了同样的问题,我只是在 viewDidLayoutSubviews 的工具栏上调用了 sizeToFit 并修复了工具栏。当我对搜索栏执行相同操作时,它仍然会部分绘制在屏幕外。 Apple 在他们的一些 iPad 应用程序中这样做,但如何做到这一点?

注意:我可以破解它以将其全部显示在屏幕上,但我必须将其包装在另一个 View 中,但颜色会消失,看起来不对劲,我认为动画已关闭。

最佳答案

您需要使用 UIBarButtonSystemItemFixedSpace 创建一个 UIBarButtonItem,宽度为 -10,这将删除 10 px 填充。

UIBarButtonItem *Spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[Spacer setWidth:-10];
UISearchBar *searchBar= [[UISearchBar alloc] initWithFrame:CGRectMake(0, 5, self.view.frame.size.width-10, 39)];
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:Spacer,searchBarItem,nil];

关于ios - 如何在 UIBarButtonItem 中放置 UISearchBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32001151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com