我希望我的搜索栏将其背景向上延伸到状态栏下方,如下所示:
这是上图对应的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.delegate = self;
[self.view addSubview:self.searchBar];
self.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = @{@"v":self.searchBar,
@"topLayoutGuide":self.topLayoutGuide};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][v]" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:views]];
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"searchbarBG"] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
}
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
当我UISearchDisplayController
通过在方法末尾附加以下行将搜索栏添加到 a 时,就会出现问题viewDidLoad
:
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
这是我得到的结果:
请注意,图像看起来被剪裁了。这与我在代码中将 UIBarPositionTopAttached 更改为 UIBarPositionTop 的样子不同:
被剪辑的图像让我想到了clipsToBounds
,我可以通过在末尾添加这段代码来正确显示搜索栏viewDidLoad
:
for (UIView *subview in self.searchBar.subviews) {
subview.clipsToBounds = NO;
}
所以我猜这UISearchDisplayController
是在弄乱背景视图的clipsToBounds
属性。UISearchBar
有没有其他人为此而苦苦挣扎?我的代码有问题吗?
更新:
早在 2013 年 10 月,除了发布这个问题,我还使用 Apple Bug Reporter 工具报告了这个问题。2015 年 1 月 6 日(太晚了)我收到了 Apple Developer Relations 的回复:
没有计划根据以下情况解决此问题:
不再支持 UISearchDisplayController。UISearchController 应该提供所需的功能,如果没有(或行为不正确),请提交一个新错误。
我们现在关闭这份报告。
如果您对解决方案有疑问,或者这对您来说仍然是一个关键问题,那么请使用该信息更新您的错误报告。
请务必定期检查新的 Apple 版本,了解可能影响此问题的任何更新。