我正在尝试重现类似于Maps应用程序的搜索界面。
我在我UISearchController
的searchBar
内部使用了它navigationBar
。我rightBarButtonItem
在导航栏上也有一个。
默认情况下,显示搜索控制器时,搜索栏的“取消”按钮显示在现有权限项目的旁边。我想要做的-就像使用Maps应用程序一样-是在显示searchController时隐藏我正确的导航项,并在关闭该项时再次显示它。
我已经成功地使用了委托方法来做到这一点,但是动画中的跳转非常丑陋,我想知道如何避免这种情况。
这是我的代码(右边的栏项目将切换慢速动画,使该错误更容易看到)
AppDelegate.h
//
// AppDelegate.h
// SearchNavigationItemAnimation
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
//
// AppDelegate.m
// SearchNavigationItemAnimation
//
#import "AppDelegate.h"
#import "SearchViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
SearchViewController *rootController = [[SearchViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
@end
SearchViewController.h
//
// SearchViewController.h
// SearchNavigationItemAnimation
//
#import <UIKit/UIKit.h>
@interface SearchViewController : UIViewController
@end
SearchViewController.m
//
// SearchViewController.m
// SearchNavigationItemAnimation
//
#import "SearchViewController.h"
@interface SearchViewController () <UISearchControllerDelegate, UISearchResultsUpdating>
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UIBarButtonItem *rightBarButtonItem;
@end
@implementation SearchViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(navigationBarButtonItemAction)];
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.navigationItem.titleView = self.searchController.searchBar;
self.navigationItem.rightBarButtonItem = self.rightBarButtonItem;
self.definesPresentationContext = YES;
}
- (void)willPresentSearchController:(UISearchController *)searchController
{
[self.navigationItem setRightBarButtonItem:nil animated:true];
}
- (void)willDismissSearchController:(UISearchController *)searchController
{
[self.navigationItem setRightBarButtonItem:self.rightBarButtonItem animated:true];
}
- (void)navigationBarButtonItemAction {
float windowLayerSpeed = [UIApplication sharedApplication].keyWindow.layer.speed;
[UIApplication sharedApplication].keyWindow.layer.speed = (windowLayerSpeed == 1.0) ? 0.1 : 1.0;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}
@end
编辑
让我再解释一下发生了什么。
如果不添加barButtonItem,则searchBar占据导航栏的整个宽度。当您触摸它时,它会激活并显示带有漂亮动画的“取消”按钮(搜索字段会缩小,为“取消”按钮从右侧滑入留有空间)。
使用导航栏上的右键,如果您不尝试以任何方式添加/删除/修改它,则其工作方式相同。搜索栏占用除按钮宽度以外的所有空间,并且激活后,取消按钮将从按钮的右侧和下方滑入并降落在其旁边(在搜索字段和现有的右侧按钮之间)。一切都是动画,可以正常工作。
现在,如果要在取消按钮滑入时使右键消失,则必须对其框架进行动画处理。问题在于,搜索栏会自动尝试占用所有可用空间,并且这样做并没有为其边框添加动画效果。因此,假设您要将按钮的帧宽度动画化为零,搜索栏将立即扩展其宽度,而不是跟随动画。
显然这是一个错误,或者如果您是更宽容的尚未实现的功能... ...但是它在iOS7中使用时效果很好UISearchDisplayController
,而Apple在Maps应用程序中就可以了。因此,我要求提供任何可行的解决方法,必要时包括私有API调用。
如果有人感兴趣,我设法使用使其工作UISearchDisplayController
。请参阅此示例项目。
不过,我仍然对适用的解决方案感兴趣UISearchController
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句