草庐IT

ios - UISearchBar 向下移动 20 像素

coder 2023-07-27 原文

我有一个 UITableViewController T,它以标准方式包含一个 UISearchDisplayControllerUISearchBar(在 nib 中定义的所有内容)。然后,其他 View Controller 以标准方式将 T 作为 subview Controller 包含在内。

搜索栏正确显示:

但是当我点击搜索栏进行实际搜索时,搜索栏会变高,搜索栏内的内容会下降大约 20 像素(呃,状态栏的高度?巧合?)像这样:

这是怎么回事?它太高了。此外,使它看起来像那样的动画很笨拙。有什么方法可以防止这种难看的生长?

最佳答案

我设法通过编程方式设置 UISearchController 来解决与您遇到的问题非常相似的问题,如下所示:

注意 ~ 如果 hidesNavigationBarDuringPresentation 未设置为 NO,错误仍然存​​在。

#pragma mark - View Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];

    self.results = [[NSArray alloc] init];

//  1  \\ Results Table View
    UITableView *searchResultsTableView = [[UITableView alloc] initWithFrame:self.tableView.frame];
    [searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Identifier];
    searchResultsTableView.delegate = self;
    searchResultsTableView.dataSource = self;

//  2  \\ init search results table view & setting its table view
    self.searchResultsTableViewController = [[UITableViewController alloc] init];
    self.searchResultsTableViewController.tableView = searchResultsTableView;
    self.searchResultsTableViewController.view.backgroundColor = [UIColor blackColor];

//  3  \\ init a search controller with it's tableview controller for results
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsTableViewController];
    self.searchController.hidesNavigationBarDuringPresentation = NO;    //  √
    self.searchController.searchResultsUpdater = self;
    self.searchController.delegate = self;
    self.searchController.searchBar.barTintColor = [UIColor blackColor];

//  4  \\ Make an appropriate search bar (size, color, attributes) and add it as the header
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.tableView.tableHeaderView.backgroundColor = [UIColor blackColor];

//  5  \\ Enable presentation context
    self.definesPresentationContext = YES;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.currentUser = [PFUser currentUser];
}

添加了更多详细信息:

  1. 界面生成器:UIViewController(不是 UITableViewController)
  2. 添加一个带有约束的UITableView:

    一个。

    *My Nav Bar 是自定义高度 - 您的“Top Space to: Top Layout Guide”可能会更改

  3. 将添加的 TableView 中的委托(delegate)和数据源导出添加到 UIViewController

    一个。不要忘记以编程方式执行此操作:

    @interface AddUsersViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    
  4. 在您的接口(interface)扩展中:

    一个。设置您的 UISearchConroller 的委托(delegate)和数据源:

    #import "AddUsersViewController.h"
    
    #define ResultsTableView self.searchResultsTableViewController.tableView
    #define Identifier @"Cell"
    
    @interface AddUsersViewController () <UISearchControllerDelegate, UISearchResultsUpdating>
    
    @property (nonatomic) UISearchController *searchController;
    @property (nonatomic) UITableViewController *searchResultsTableViewController;
    @property (nonatomic, weak) UIBarButtonItem *leftBarButton;
    @property (nonatomic) NSArray *results;
    
    @end
    

虽然这对我有用,但我发现还有其他可能的情况会导致此问题。我的“问题”不是搜索栏向下移动了 20 像素(左右),而是它向上移动了。我将此归因于包含图像标题和自定义 NavBarButtons 的自定义 UINavigationBar header 。考虑到这一点,这里还有两个可能的解决方案:

.

原始场景:

  1. 您的容器 View 的 extendedLayoutIncludesOpaqueBars 属性

    一个。 Storyboard:选中 √“在不透明条下”以获取 Storyboard中的 TableView 。

    以编程方式:yourTableView(s).extendedLayoutIncludesOpaqueBars = YES;

.

  1. scopeButtonTitles 设置为一个空数组。 (下)

    self.searchController = UISearchController(searchResultsController: nil);
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.scopeButtonTitles = [];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    

后两个答案来自THIS问题,并可能证明对检查也有好处。

感谢 BananaNeil 的研究和测试,他发现如果您遵循我的建议并设置您的 searchController.searchBar.y = 0,您的搜索栏应该正好位于您希望的位置。 √

关于ios - UISearchBar 向下移动 20 像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21893983/

有关ios - UISearchBar 向下移动 20 像素的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. ruby-on-rails - 如何在 Rails Controller Action 上触发 Facebook 像素 - 2

    我有一个ruby​​onrails应用程序。我按照facebook的说明添加了一个像素。但是,要跟踪转化,Facebook要求您将页面置于达到预期结果时出现的转化中。即,如果我想显示客户已注册,我会将您注册后转到的页面作为成功对象进行跟踪。我的问题是,当客户注册时,在我的应用程序中没有登陆页面。该应用程序将用户带回主页。它在主页上显示了一条消息,所以我想看看是否有一种方法可以跟踪来自Controller操作而不是实际页面的转化。我需要计数的Action没有页面,它们是ControllerAction。是否有任何人都知道的关于如何执行此操作的gem、文档或最佳实践?这是进入布局文件的像素

  4. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  8. kvm虚拟机安装centos7基于ubuntu20.04系统 - 2

    需求:要创建虚拟机,就需要给他提供一个虚拟的磁盘,我们就在/opt目录下创建一个10G大小的raw格式的虚拟磁盘CentOS-7-x86_64.raw命令格式:qemu-imgcreate-f磁盘格式磁盘名称磁盘大小qemu-imgcreate-f磁盘格式-o?1.创建磁盘qemu-imgcreate-fraw/opt/CentOS-7-x86_64.raw10G执行效果#ls/opt/CentOS-7-x86_64.raw2.安装虚拟机使用virt-install命令,基于我们提供的系统镜像和虚拟磁盘来创建一个虚拟机,另外在创建虚拟机之前,提前打开vnc客户端,在创建虚拟机的时候,通过vnc

  9. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  10. ruby-on-rails - rails : Find tasks that were created on a certain day? - 2

    我有一个任务列表(名称、starts_at),我试图在每日View中显示它们(就像iCal)。deftodays_tasks(day)Task.find(:all,:conditions=>["starts_atbetween?and?",day.beginning,day.ending]end我不知道如何将Time.now(例如“2009-04-1210:00:00”)动态转换为一天的开始(和结束),以便进行比较。 最佳答案 deftodays_tasks(now=Time.now)Task.find(:all,:conditio

随机推荐