代码之家  ›  专栏  ›  技术社区  ›  heyfrank

滚动条上没有隐藏UISearchBar书签图标

  •  0
  • heyfrank  · 技术社区  · 6 年前

    UISearchBar 与此代码集成在我的导航栏中:

    // not needed because it's default
    navigationItem.hidesSearchBarWhenScrolling = true
    
    self.navigationItem.searchController = searchController
    

    我还添加了一个自定义书签图标,如下所示:

    searchController.searchBar.setImage(icon, for: .bookmark, state: .normal)
    searchController.searchBar.showsBookmarkButton = true
    searchController.searchBar.layoutIfNeeded()
    

    enter image description here

    似乎文本字段不会剪辑图标,也不会像对占位符和搜索图标那样应用淡入淡出动画。

    如果没有,有人能复制吗?

    0 回复  |  直到 6 年前
        1
  •  1
  •   heyfrank    6 年前

    好吧,我通过这样做来解决这个问题:

    searchController.searchBar.allSubviews.forEach { $0.clipsToBounds = true }
    

    使用此扩展名获取所有嵌套子视图:

    extension UIView {
        var allSubviews: [UIView] {
            return self.subviews.reduce([UIView]()) { $0 + [$1] + $1.allSubviews }
        }
    }
    

        2
  •  0
  •   Robert Hovhannisyan    6 年前

    尝试使用 clipToBounds layoutIfNeeded()

    searchController.searchBar.setImage(icon, for: .bookmark, state: .normal)
    searchController.searchBar.showsBookmarkButton = true
    searchController.searchBar.clipsToBounds = true