聚焦时如何调整 UISearchController.searchBar 的大小问题? [英] How to resizing issue of UISearchController.searchBar when focused?

查看:150
本文介绍了聚焦时如何调整 UISearchController.searchBar 的大小问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们很久以前就为 iOS 5 开发了一个应用程序,我们在 storyboard 上使用了搜索栏和搜索显示控制器"对象.由于 iOS8 UISearchDisplayController 已被弃用,我正在尝试使用 UISearchController 删除现有的搜索栏和搜索显示控制器".

We have developed an application for iOS 5 long back where we used 'Search Bar and Search Display Controller' object on storyboard. Since iOS8 UISearchDisplayController is deprecated, I am trying to remove existing 'Search Bar and Search Display Controller' with UISearchController.

由于 UISearchController 在界面上的对象库"中不可用,我已使用以下代码以编程方式将其添加到 interface 上的 UIView:

As UISearchController is not available from 'Object Library' on interface, I have added it programatically to an UIView on interface using following code:

//Set up Search Controller
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = (id)self;
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.searchBar.delegate = self;

//Adding as subview to UIView 'searchBarContainer'
[self.searchBarContainer addSubview:self.searchController.searchBar];
[self.searchBarContainer bringSubviewToFront:self.searchController.searchBar];

self.definesPresentationContext = YES;
self.extendedLayoutIncludesOpaqueBars = YES;

然后将 'searchController' 的 frame 设置为等于 UIView,如下所示:

And set the frame of 'searchController' equal to the UIView as follows:

UISearchBar *ctrlSearchBar = self.searchController.searchBar;
ctrlSearchBar.frame = CGRectMake(0, 0, self.searchBarContainer.frame.size.width, self.searchBarContainer.frame.size.height);

searchBar 的大小是可以的,但是当它专注于点击它时,'searchBar' 的宽度会自动增加.

The size of searchBar is fine but when it is focused on clicking on it, the width of 'searchBar' increased automatically.

我什至尝试使用 NSLayoutConstraints 但没有解决问题.任何人都可以帮忙修复UISearchController.searchBar"的大小,即使它是聚焦的?

I even tried using NSLayoutConstraints but didn't fix the issue. Can anyone please help how to fix the size of 'UISearchController.searchBar' even when it is focused?

推荐答案

正如 surToTheW 在他的评论中建议的那样,我有创建了一个带有 UISearchController 的自定义 UIViewController.我将它作为子视图控制器添加到我的主 UIViewController 中,并且搜索栏没有超过自定义 UIViewController 的视图宽度.

As surToTheW suggested in his comment, I have created a custom UIViewController with UISearchController in it. I added it as child view controller in to my main UIViewController and the search bar didn't exceed the custom UIViewController's view width.

这篇关于聚焦时如何调整 UISearchController.searchBar 的大小问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆