如何在iOS 11上去掉UISearchController底部的线条?

3

我该如何在iOS 11上删除UISearchController下面的线?

我使用以下代码添加了UISearchController

navigationItem.searchController = searchController

但是在这样做之后,下面会有一条奇怪的线: enter image description here 如果您能提供如何去除该线或至少选择其颜色的建议,将不胜感激。

我认为这就是所谓的“毛细线”。搜索该术语,您会找到各种解决方案。 - koen
4个回答

5

这里有另一个解决方案,虽然不太强大,但在特定的用例中可能会很有用。

extension UISearchController {

    var hairlineView: UIView? {
        guard let barBackgroundView = self.searchBar.superview?.subviews.filter({ String(describing: type(of: $0)) == "_UIBarBackground" }).first
        else { return nil }

        return barBackgroundView.subviews.filter({ $0.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更新中出现问题。
此外,$0.bounds.height == 1 / self.traitCollection.displayScale是不安全的,我建议您使用适当的浮点数比较方法


谢谢Vin的贡献。由于它是目前最好的,我会将其标记为正确的 :) - budiDino

2
一个取巧的解决方案,但是目前最好的办法是添加一个白色线条覆盖在黑色线条上方。
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)

你找到更简洁的解决方案了吗? - Vin Gazoil
@VinGazoil 很遗憾,没有。最终在右上角添加了一个搜索图标,打开一个带有搜索功能的新屏幕。如果你最终解决了这个奇怪的问题,请回来发表你的答案。祝好运 ;) - budiDino

0

如果你将其添加为子视图,当按返回或前往另一页时可能会出现故障。这可以改进你的解决方案@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

0

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]];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接