如何删除 iOS 11 上 UISearchController 下的行?
How to remove the line under the UISearchController on iOS 11?
如何删除 iOS 11 上 UISearchController
下的行?
我使用以下代码添加了 UISearchController
:
navigationItem.searchController = searchController
但是这样做之后下面有一条奇怪的线:
任何关于如何删除线条或至少选择其颜色的建议将不胜感激。
一个 hacky 解决方案,但我目前拥有的最好的解决方案是添加一个与黑线重叠的白线视图:
let lineView = UIView(frame: CGRect(x: 0, y: searchController.searchBar.frame.height-4, width: view.bounds.width, height: 1))
lineView.backgroundColor = .white
searchController.searchBar.addSubview(lineView)
这是另一种解决方案,一个较弱的解决方案,但在特定用例中可能很有用。
extension UISearchController {
var hairlineView: UIView? {
guard let barBackgroundView = self.searchBar.superview?.subviews.filter({ String(describing: type(of: [=10=])) == "_UIBarBackground" }).first
else { return nil }
return barBackgroundView.subviews.filter({ [=10=].bounds.height == 1 / self.traitCollection.displayScale }).first
}
}
使用该扩展,您只需在视图控制器的 viewWillLayoutSubviews
方法中编写以下代码:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// Remove hairline from the searchBar.
self.navigationItem.searchController?.hairlineView?.isHidden = true
}
重要:
请注意,此解决方案非常薄弱,可能会随着将来的 iOS 更新而中断。
此外,[=13=].bounds.height == 1 / self.traitCollection.displayScale
是不安全的,我建议您使用 a proper float comparison method.
如果您将其添加为子视图,您会发现在按后退或前进到另一页时出现故障。这可以改进您的解决方案@budidino
let lineView = UIView(frame: .zero)
lineView.backgroundColor = .white
searchController.searchBar.addSubview(lineView)
lineView.translatesAutoresizingMaskIntoConstraints = false
lineView.leadingAnchor.constraint(equalTo: searchController.searchBar.leadingAnchor).isActive = true
lineView.trailingAnchor.constraint(equalTo: searchController.searchBar.trailingAnchor).isActive = true
lineView.bottomAnchor.constraint(equalTo: searchController.searchBar.bottomAnchor, constant: 1).isActive = true
lineView.heightAnchor.constraint(equalToConstant: 1).isActive = true
在iOS11
@interface UISearchController (Additions)
- (void)hideHairLineView;
@end
@implementation UISearchController (Additions)
- (void)hideHairLineView{
UIView *barBackgroundView = self.searchBar.superview.subviews.firstObject;
for(UIView *v in barBackgroundView.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
UIImageView *imgView= (UIImageView *)v;
if (imgView.frame.size.height <= 1.0) {
[imgView setHidden:YES];
}
}
}
}
在viewWillLayoutSubviews
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self.navigationItem.searchController hideHairLineView];
}
在iOS13
来自viewDidLoad
添加
[self.navigationController setDelegate:self];
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController.navigationController.navigationBar.viewForLastBaselineLayout setBackgroundColor:[UIColor clearColor]];
}
如何删除 iOS 11 上 UISearchController
下的行?
我使用以下代码添加了 UISearchController
:
navigationItem.searchController = searchController
但是这样做之后下面有一条奇怪的线:
任何关于如何删除线条或至少选择其颜色的建议将不胜感激。
一个 hacky 解决方案,但我目前拥有的最好的解决方案是添加一个与黑线重叠的白线视图:
let lineView = UIView(frame: CGRect(x: 0, y: searchController.searchBar.frame.height-4, width: view.bounds.width, height: 1))
lineView.backgroundColor = .white
searchController.searchBar.addSubview(lineView)
这是另一种解决方案,一个较弱的解决方案,但在特定用例中可能很有用。
extension UISearchController {
var hairlineView: UIView? {
guard let barBackgroundView = self.searchBar.superview?.subviews.filter({ String(describing: type(of: [=10=])) == "_UIBarBackground" }).first
else { return nil }
return barBackgroundView.subviews.filter({ [=10=].bounds.height == 1 / self.traitCollection.displayScale }).first
}
}
使用该扩展,您只需在视图控制器的 viewWillLayoutSubviews
方法中编写以下代码:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// Remove hairline from the searchBar.
self.navigationItem.searchController?.hairlineView?.isHidden = true
}
重要:
请注意,此解决方案非常薄弱,可能会随着将来的 iOS 更新而中断。
此外,[=13=].bounds.height == 1 / self.traitCollection.displayScale
是不安全的,我建议您使用 a proper float comparison method.
如果您将其添加为子视图,您会发现在按后退或前进到另一页时出现故障。这可以改进您的解决方案@budidino
let lineView = UIView(frame: .zero)
lineView.backgroundColor = .white
searchController.searchBar.addSubview(lineView)
lineView.translatesAutoresizingMaskIntoConstraints = false
lineView.leadingAnchor.constraint(equalTo: searchController.searchBar.leadingAnchor).isActive = true
lineView.trailingAnchor.constraint(equalTo: searchController.searchBar.trailingAnchor).isActive = true
lineView.bottomAnchor.constraint(equalTo: searchController.searchBar.bottomAnchor, constant: 1).isActive = true
lineView.heightAnchor.constraint(equalToConstant: 1).isActive = true
在iOS11
@interface UISearchController (Additions)
- (void)hideHairLineView;
@end
@implementation UISearchController (Additions)
- (void)hideHairLineView{
UIView *barBackgroundView = self.searchBar.superview.subviews.firstObject;
for(UIView *v in barBackgroundView.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
UIImageView *imgView= (UIImageView *)v;
if (imgView.frame.size.height <= 1.0) {
[imgView setHidden:YES];
}
}
}
}
在viewWillLayoutSubviews
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self.navigationItem.searchController hideHairLineView];
}
在iOS13
来自viewDidLoad
添加
[self.navigationController setDelegate:self];
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController.navigationController.navigationBar.viewForLastBaselineLayout setBackgroundColor:[UIColor clearColor]];
}