【问题标题】:How to remove the line under the UISearchController on iOS 11?如何在 iOS 11 上删除 UISearchController 下的行?
【发布时间】:2017-10-18 12:12:06
【问题描述】:

如何在 iOS 11 上删除 UISearchController 下的行?

我使用此代码添加了UISearchController

navigationItem.searchController = searchController

但是在它下面有一条奇怪的线:

任何关于如何删除线或至少选择其颜色的建议将不胜感激。

【问题讨论】:

  • 我认为这就是所谓的hairline。搜索该术语,您会找到各种解决方案。

标签: swift ios11 uinavigationitem uisearchcontroller


【解决方案1】:

这是另一种解决方案,虽然很弱,但在特定用例中可能很有用。

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 是不安全的,我建议您使用a proper float comparison method

【讨论】:

  • 感谢 Vin 的贡献。我会将其标记为正确,因为它是我们目前拥有的最好的:)
【解决方案2】:

一个 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)

【讨论】:

  • 您找到更清洁的解决方案了吗?
  • @VinGazoil 遗憾的是没有。最后在右上角添加了一个搜索图标,该图标打开了一个具有搜索功能的新屏幕。如果您最终解决了这个奇怪的问题,请回来并发布您的答案。祝你好运;)
【解决方案3】:

如果您将其添加为子视图,则在按返回或前进到另一个页面时会发现故障。这可以改善您的解决方案@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

【讨论】:

    【解决方案4】:

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

    【讨论】: