我以编程方式创建了一个 UISearchController,并在 viewDidLoad 中设置范围按钮标题
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
[searchResultsController.tableView registerNib:musicNib forCellReuseIdentifier:@"MusicCell"];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
UISearchBar *searchBar = self.searchController.searchBar;
searchBar.scopeButtonTitles = @[@"Album", @"Artist", @"Song"];
searchBar.showsScopeBar = YES;
searchBar.selectedScopeButtonIndex = 0;
searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
Run Code Online (Sandbox Code Playgroud)
但是当我在加载视图时在搜索栏中搜索时。范围栏不显示。我在搜索栏中搜索的第二次,范围栏显示。这是怎么回事?

我想将UISearchBar的Cancel按钮更改为只有图像而没有文本的按钮。这就是我远离原始行为的地方
对此
朝着正确的方向迈出了一步,但问题是按钮太宽了。当我调试视图时,我可以看到它的左侧和右侧有 11 个点的按钮标签插入。有谁知道如何使按钮适合内容大小?图像是正方形的。
这是我的自定义按钮的代码:
UIBarButtonItem *barButtonAppearanceInSearchBar;
if (IOS9) {
barButtonAppearanceInSearchBar = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]];
} else {
barButtonAppearanceInSearchBar = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
}
barButtonAppearanceInSearchBar.image = [UIImage imageNamed:@"Close"];
barButtonAppearanceInSearchBar.title = nil;
Run Code Online (Sandbox Code Playgroud)
另一个奇怪的问题是,当我关闭搜索栏并再次激活它时,按钮图像变暗(它仍然存在,我在调试视图时可以看到它),所以它看起来像这样
知道如何使图标保持白色吗?我尝试了以下方法,但没有结果:
- (void)willPresentSearchController:(UISearchController *)searchController {
searchController.searchBar.tintColor = [UIColor whiteColor];
}
Run Code Online (Sandbox Code Playgroud) objective-c uibarbuttonitem uisearchbar ios uisearchcontroller
是否可以使用UISearchController而不SearchBar显示在 中Navigation Controller?我基本上希望SearchBar保留在屏幕上并在其下方显示 TableView:
我想做的是:我UITableViewController在故事板上创建了一个并将其链接到自定义NPTableViewController类。然后我这样做了:
let resultsTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearch") as! NPTableViewController
searchController = UISearchController(searchResultsController: resultsTable)
searchController?.searchResultsUpdater = resultsTable
let searchBar = searchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
searchBar.frame.origin.y = 100.0
self.view.addSubview(searchBar)
Run Code Online (Sandbox Code Playgroud)
现在,当我运行它时,会searchBar显示 ,但是当我单击它时,背景会变暗,searchBar消失,我的resultsTable也不显示。
iphone uinavigationcontroller uisearchbar swift uisearchcontroller
这里和这里已经有 2 个关于这个主题的未答复的帖子,没有人为这个问题提供有效的解决方案。
第一张图片显示了搜索控制器的 iOS 10 行为,您可以在其中看到左侧的层次结构和右侧的红色框内的搜索控制器框架。中心蓝色的视图是表中正确显示的视图。

第二张图显示了 iOS 11 的行为,其中搜索控制器退出标题边界tableView,并移动到屏幕顶部,这是没有意义的,因为表格视图位于距屏幕顶部 230 像素的位置。

我用来显示所有这些的代码如下(在 中viewDidLoad):
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.clipsToBounds = YES;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchBar.placeholder = @"Enter name or email";
[self.searchController.searchBar sizeToFit];
Run Code Online (Sandbox Code Playgroud) 我UISearchController在一个针对 iOS 10+ 的项目中使用,我可以将它设置navigationItem.searchController为 iOS 11,但对于 iOS 10,我必须手动添加搜索栏。
在 iOS 10 上存在一个问题,当obscuresBackgroundDuringPresentation设置为 时true,当搜索栏成为第一响应者时,覆盖层会变暗,但它不仅涵盖搜索结果区域,还涵盖搜索栏本身。
在 iOS 11 上它可以完美运行。
有什么我必须设置/更改的吗?
谢谢!
这一直有效,直到 tvOS 最近更新为止。有什么想法发生了什么吗?搜索栏仍然存在于应用程序中,它只是直接跳到桌面视图。
当应用程序首次发布时,一切都工作正常,但在某些时候,它就停止工作了。我在控制台中没有收到任何错误,一切都表明它已分配并初始化。
-(void)viewDidLoad {
[super viewDidLoad];
_resultsTableController = [[APLResultsTableController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed
}
- (void)viewWillAppear:(BOOL)animated {
if (self.searchControllerWasActive) {
self.searchController.active = self.searchControllerWasActive;
_searchControllerWasActive = NO;
if (self.searchControllerSearchFieldWasFirstResponder) {
[self.searchController.searchBar becomeFirstResponder];
_searchControllerSearchFieldWasFirstResponder = NO;
}
}
NSBundle …Run Code Online (Sandbox Code Playgroud) 我有一个 UISearchController 和 searchBar 分配给我的视图控制器的导航项。在 iOS 13.3 中一切正常。但是,在 iOS 12.4 中,searchBar 缺失并且似乎尚未添加到视图层次结构中。
这是 iOS 13.3 中的 UI:
在 iOS 12.4 中也是如此:
我配置搜索控制器的代码如下:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.obscuresBackgroundDuringPresentation = NO;
NSString *placeholder = NSLocalizedString(@"Address or place name", nil);
self.searchController.searchBar.placeholder = placeholder;
self.searchController.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchController.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
self.navigationItem.hidesSearchBarWhenScrolling = NO;
self.navigationItem.searchController = self.searchController;
[self.searchController.searchBar sizeToFit];
Run Code Online (Sandbox Code Playgroud)
如果我通过在调试器中输入以下内容来检查 searchBar 的视图层次结构:
po self.navigationItem.searchController.searchBar.superview.superview
Run Code Online (Sandbox Code Playgroud)
我在 iOS 13.3 中看到以下内容:
<UILayoutContainerView: 0x7f8c97b300c0; frame = (0 0; 375 …Run Code Online (Sandbox Code Playgroud) interface-builder uisearchbar uinavigationitem ios uisearchcontroller
我正在尝试UISearchControlleriOS 8中引入的新API.尽管API是新的,但它使用的是相同的旧API UISearchBar.我把它添加到UINavigationController的titleView.

import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
navigationItem.titleView = searchController.searchBar
}
}
Run Code Online (Sandbox Code Playgroud)
我不需要搜索栏来获取导航栏的整个宽度.问题是我无法调整它的大小.首先,我尝试设置搜索栏的框架.
searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 100, height: 44)
Run Code Online (Sandbox Code Playgroud)
这没用,所以我试着设置titleView框架.
navigationItem.titleView!.frame = CGRect(x: 0, y: 0, width: 100, height: 44)
Run Code Online (Sandbox Code Playgroud)
它们都不起作用.
有谁知道另一种方法吗?
谢谢.
任何人都已经成功实施UIViewController该contais既有UISearchController searchBar和UItableView在使用自动布局铺设东西展现出来?
我正在努力实现类似于1Password在iPhone上所做的事情:固定searchBar在一个tableView(不是它的一部分)之上tableHeaderView.当UISearchController拥有searchBar激活时,它的searchBar动画显示范围按钮,从而tableView向下移动一点.
我已经使用此类正确使用此布局的基础知识:
//
// ViewController.m
// UISearchControllerIssues
//
// Created by Aloha Silver on 05/02/16.
// Copyright © 2016 ABC. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UISearchResultsUpdating, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupTableView];
[self setupSearchInterface];
[self setupConstraints]; …Run Code Online (Sandbox Code Playgroud) 我正在UISearchController为自己实现一个,UITableView但我正在为iOS 11的自定义设置而苦苦挣扎。我的导航栏使用的是渐变图像背景,我希望搜索控制器能够匹配该渐变背景,但是我还没有找到一种设置背景图像的方法为UISearchController。它可以作为TableHeaderView在UISearchController上完美运行,但是在iOS 11中几乎没有传递任何自定义设置。
当前结果:
期望的结果:
这是我正在使用的代码:( 在viewDidLoad上调用)
private func setupSearchController() {
let searchController = UISearchController(searchResultsController: nil)
// Convert CAGradientLayer to UIImage
let gradient = Color.blue.gradient
gradient.frame = searchController.searchBar.bounds
UIGraphicsBeginImageContext(gradient.bounds.size)
gradient.render(in: UIGraphicsGetCurrentContext()!)
let gradientImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Setup the Search Controller
searchController.searchBar.backgroundImage = gradientImage
searchController.searchBar.isTranslucent = false
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search by transaction name"
searchController.searchBar.tintColor = Color.custom(hexString: "FAFAFA", alpha: 1).value
definesPresentationContext = true …Run Code Online (Sandbox Code Playgroud) uisearchbar ×8
ios ×7
swift ×4
objective-c ×3
uitableview ×2
ios8 ×1
iphone ×1
tvos ×1
xcode ×1