我添加了一个搜索栏和搜索显示控制器(假设这是我在storyboard中添加的,因为他们没有'SearchController'对象被拖出来).
我的问题是如何在SearchController上设置属性呢?self.searchController不起作用,Xcode表示当我将UISearchBar添加到视图时会自动添加它.我究竟做错了什么?我需要访问它的原因是我可以设置结果搜索表,如下所示:
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
Run Code Online (Sandbox Code Playgroud)
我注意到这里的例子:https://github.com/dempseyatgithub/Sample-UISearchController
其中说它使用故事板,实际上并没有使用故事板来添加UISearchController,而是通过分配和初始化以及设置框架来使用代码.
我在iOS中使用UISearchBar遇到奇怪的行为。我的搜索栏工作正常:
但是,当我UINavigationBar.appearance().translucent = false在ApplicationDelegate中didFinishLaunchingWithOptions进行设置以使颜色正确显示在iOS应用程序的导航栏中时,搜索栏上方会出现一个白色部分,如下所示:
滚动结果表视图时,基础内容在该间隙中可见:
我要修复的最接近的是setting mySearchResultsViewController.edgesForExtendedLayout = UIRectEdge.None,它停止了半透明的间隙,但仍然没有正确的颜色:
有什么办法可以防止它显示出变色的间隙并仍然具有全局不透明的导航栏?
有谁知道为什么不在这里调用它?谢谢。我想我正确设置了代表。
class LocationSearchController: UIViewController, UISearchResultsUpdating, UINavigationBarDelegate, UISearchControllerDelegate {
let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 44))
var locationSearchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
locationSearchController = UISearchController(searchResultsController: nil)
self.locationSearchController.loadViewIfNeeded()
//sets up search controller
self.locationSearchController.searchResultsUpdater = self
self.locationSearchController.delegate = self
definesPresentationContext = true
//self.videoSearchController.searchBar.delegate = self
//sets up nav bar
navigationBar.backgroundColor = UIColor.blackColor()
navigationBar.delegate = self
navigationItem.titleView = locationSearchController.searchBar
//leftButton.title = "Left"
//navigationItem.leftBarButtonItem = leftButton
navigationBar.items = [navigationItem]
self.view.addSubview(navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func …Run Code Online (Sandbox Code Playgroud) iphone ios uisearchresultscontroller swift uisearchcontroller
我已经实现UISearchController了它UINavigationBar在点击搜索图标时出现在上方。过滤表格后,用户应该能够选择将推送新视图控制器的行。问题是这样做时,搜索控制器仍然显示在导航栏上方。
我已经研究过这个,建议你应该设置self.definedPresentationContext为true. 当搜索栏显示在导航栏上方时,这不起作用。当您点击搜索图标时,键盘会出现,但搜索栏没有动画显示。
我发现了另一种选择是解雇的搜索控制器viewWillDisappear,通过设置active到false。这个解决方案的问题是它在推送过渡期间动画消失。当您返回时,搜索显然不再处于活动状态,因此用户丢失了搜索上下文并显示了所有结果。
我想知道如何推送视图控制器,保持搜索处于活动状态,但不显示下一个视图控制器的搜索界面。理想情况下,当新的视图控制器被推动时,搜索界面会被推到左边,并在滑回搜索结果时返回。
我已经调整了 Apple 的 UIKit Catalog 示例代码以显示不受欢迎的行为。要重现该问题,请下载此项目,在 iPhone 上运行它,点击后退按钮,选择搜索,选择在导航栏上显示,点击搜索图标,输入搜索词,然后选择结果。请注意,搜索栏仍然可见。这是来自的代码SearchPresentOverNavigationBarViewController.swift:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if searchController.active {
//searchController.active = false //undesirably dismisses search, animates away during push, upon going back search is not active
}
}
@IBAction func searchButtonClicked(button: UIBarButtonItem) {
//self.definesPresentationContext = true //causes the search to not appear
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个屏幕,其中将有一个覆盖整个屏幕的Google地图和顶部显示所选地址(标记指向的位置)的按钮.我希望当用户点击该按钮以显示UISearchBar时,他可以使用Google Places API搜索位置.
我一直在关注以下教程:https: //www.youtube.com/watch?v = gRQUoHleCGM
不幸的是,在我的情况下,搜索栏没有出现(虽然当我按下按钮时背景变暗).
以下是我按下按钮之前的一些截图.
这是我的视图控制器的代码.
class ChooseLocationViewController: UIViewController, GMSMapViewDelegate, UISearchBarDelegate, UISearchControllerDelegate {
let geocoder = GMSGeocoder()
var mapView: GMSMapView = GMSMapView.mapWithFrame(CGRectZero, camera:GMSCameraPosition.cameraWithLatitude(51.515339, longitude: -0.141838, zoom: 16))
var addressButton: UIButton = UIButton()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let marker = UIImageView(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
marker.image = UIImage(named: "default-marker")
marker.center.x = self.view.center.x
marker.center.y = self.view.center.y - (self.navigationController?.navigationBar.frame.size.height)!
addressButton = UIButton(frame: CGRect(x: 20, y: …Run Code Online (Sandbox Code Playgroud) UISearchContainerViewController存在于tvOS和iOS上.
Apple提供了示例代码,展示了如何在tvOS中使用它:它们配置UISearchController,将其交给UISearchContainerViewController,将其包装在UINavigationController中,并使其成为UITabBarController的子项之一.
但我从来没有在iOS上看过UISearchContainerViewController的例子,我无法在那里工作.例如,我完全像Apple那样做,除了我将UISearchContainerViewController推送到导航堆栈,或者我将它包装在导航控制器中并呈现它,或者其他什么; 并且没有搜索字段,所以整个事情都没用.
有没有人得到UISearchContainerViewController在iOS上做任何有用的事情?
navigationBar.frame.size.height 是 44.0f;
但 UISearchController.searchBar.height 是 56.0f;
[self.view addSubview:self.searchController.searchBar];
如何在 iOS11 中更改 UISearchController.searchBar.height ?searchBar.frame.size.height = 35 没用
我试图用来UISearchController在iOS 11中与范围栏一起显示搜索栏。
这是我用来设置搜索控制器的代码
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.showsScopeBar = true
searchController.searchBar.scopeButtonTitles = ["1", "2", "3", "4"]
searchController.searchBar.delegate = self
definesPresentationContext = true
Run Code Online (Sandbox Code Playgroud)
我想要一个带有始终可见的范围栏的搜索栏。加载视图控制器时,上面的代码可以正常工作,并显示搜索栏和范围栏。
但是,一旦搜索控制器变为活动状态然后被关闭,iOS将在关闭搜索控制器时隐藏范围栏,并且仅显示搜索栏。
我试图通过在中添加以下代码来解决此问题didDismissSearchController,但合并范围栏和搜索栏位于彼此顶部,而不是合并范围栏位于搜索栏下方(如下图所示)。将此代码添加到searchBarTextDidBeginEditing(_ searchBar: UISearchBar)或searchBarTextDidEndEditing(_ searchBar: UISearchBar)无效。
searchController.searchBar.showsScopeBar = true
searchController.searchBar.sizeToFit()
Run Code Online (Sandbox Code Playgroud)
我有一个我在UISearchBar下面附加了一个自定义视图,它提供了一些用于过滤和排序的按钮......现在我想使用 ios11 UISearchController,它提供了一个可以平滑地隐藏到顶部导航栏中的搜索栏。我环顾四周并测试了一些东西,但无法正确解决。向上滚动时,自定义视图也应该隐藏。
任何提示或任何我可以阅读的内容来掌握这个?(我对ios开发比较陌生)
我想以编程方式显示/隐藏 UISearchBar,但我没有想要的结果。
我想要做的是在向下滚动时隐藏搜索栏并在向上滚动时显示它:
我这样设置我的 UISearchBar:
var mySearchcontroller = UISearchController(searchResultsController: nil)
mySearchcontroller.obscuresBackgroundDuringPresentation = false
mySearchcontroller.searchBar.placeholder = "search"
mySearchcontroller.searchBar.delegate = self
definesPresentationContext = true
self.navigationItem.searchController = mySearchcontroller
self.navigationItem.hidesBackButton = true
self.navigationItem.hidesSearchBarWhenScrolling = false
Run Code Online (Sandbox Code Playgroud)
结果是
我实现了 scrollViewDidScroll 以使搜索栏在滚动时显示或隐藏:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if(scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0)
{
navigationItem.hidesSearchBarWhenScrolling = false
}
else
{
navigationItem.hidesSearchBarWhenScrolling = true
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
当您意识到 GRP 标签或分段控件被搜索栏屏蔽时,搜索栏下推所有控件的效果并不相同(图 1)。
我该如何解决这个问题?