我的应用在所有设备上看起来都很好,直到我在新的 iPhone X 上测试它(见附件截图)。搜索栏在导航栏下,很糟糕。我尝试使用新的安全区域插图 edgeForExtendedLayout,但没有成功。也许有人可以帮助解决这个问题。
问问题
880 次
2 回答
0
Apple 发布的关于 iPhone X(以及 8 和 8 Plus)开发的短视频解决了这种确切的情况(或非常接近的情况)。
在他们为 iPhone X 改编 WWDC 应用程序的案例研究中,他们发现搜索栏未正确设置在“视频”选项卡中。那是因为它们是present
ing UISearchController
,但在 iOS 11(适用于所有设备)中处理此问题的最佳方法是将搜索控制器附加到导航项。如果您仍需要部署回 iOS 10(或更早版本),您可以通过可用性检查来处理此问题:
let searchController = UISearchController(searchResultsController: nil)
// configure searchController properties
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
searchController.isActive = true // to show it now
} else {
present(searchController, animated: true, completion: nil)
}
同样,如果您直接将搜索栏设置为表格视图的标题,您也可以将其附加到导航项:
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
searchController.isActive = shouldShowBarNow // made up local variable
} else {
if shouldShowBarNow {
self.tableView.tableHeaderView = searchController.searchBar
} else {
self.tableView.tableHeaderView = nil
}
}
于 2017-09-20T22:48:12.430 回答
0
经过一番调查,我想出了如何解决我的问题。我必须根据安全区域对齐搜索视图。请参阅以下解决方案:
if (@available(iOS 11.0, *))
{
[self.searchView setTranslatesAutoresizingMaskIntoConstraints:NO];
UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[self.searchView.topAnchor constraintEqualToAnchor:guide.topAnchor],
[self.searchView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor],
[self.searchView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor],
[NSLayoutConstraint constraintWithItem:self.searchView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44]
]];
}
现在,所有设备上的一切看起来都很棒。希望这可以帮助其他人解决类似的问题。干杯。
于 2017-09-29T09:29:12.480 回答