【问题标题】:Customize UISearchBar: Trying to get rid of the 1px black line underneath the search bar自定义 UISearchBar:试图摆脱搜索栏下方的 1px 黑线
【发布时间】:2011-10-01 13:34:19
【问题描述】:

我的问题已经在标题中说明了:我想去掉UISearchBar底部画的黑线。有什么想法吗?

这是我的意思的图像:

更新

我认为该行是UITableViewtableHeaderView 的一部分。我仍然不知道如何删除它。

【问题讨论】:

标签: iphone ios ipad uisearchbar


【解决方案1】:

试试这个

 searchBar.layer.borderWidth = 1;

 searchBar.layer.borderColor = [[UIColor lightGrayColor] CGColor];

【讨论】:

  • 对我有用,在我的情况下,我使用的是自定义条形颜色,所以我的代码如下(swift):searchBar.layer.borderColor = searchBar.barTintColor?.CGColor
  • 错误地设置了“searchBar.layer.borderColor = searchBar.tintColor?.CGColor”
  • 有人说要添加searchBar.layer.borderColor = searchBar.barTintColor?.CGColor,有人说要删除-_-
【解决方案2】:

为什么:

所以,我已经深入研究了 API,试图找出发生这种情况的原因。显然,编写UISearchBar API 的人正在将线条光栅化到图像上并将其设置为backgroundImage

解决方案:

我提出一个更简单的解决方案,如果你想设置backgroundColor并摆脱细线:

searchBar.backgroundColor = <#... some color #>
searchBar.backgroundImage = [UIImage new];

或者,如果您只需要没有细线的背景图片:

searchBar.backgroundImage = <#... some image #>

【讨论】:

  • 对于 iOS7+,您需要使用 setBackgroundImage:forBarPosition:barMetrics: 方法。
  • 此解决方案删除栏。谢谢@Oxcug!
【解决方案3】:

我的UISearchBar 的顶部和底部都有0.5px 黑色水平线。到目前为止,我摆脱它们的唯一方法是将其样式设置为 Minimal

mySearchBar.searchBarStyle = UISearchBarStyleMinimal;

【讨论】:

  • 这会使 serachbar 内的文本字段变灰
【解决方案4】:

解决方案

XCode 10.1 斯威夫特 4.2

【讨论】:

  • 太棒了,你成就了我的一天。
【解决方案5】:

我通过将subview 添加到searchBar 的视图堆栈来解决此问题,如下所示:

CGRect rect = self.searchBar.frame;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, rect.size.height-2,rect.size.width, 2)];
lineView.backgroundColor = [UIColor whiteColor];
[self.searchBar addSubview:lineView];

这里,self.searchBar 是我的控制器类的 UISearchBar 指针。

【讨论】:

  • 我在 UISearchBar 的子类的 layoutSubviews 方法中添加了这一行。无论如何,我正在使用子类。
【解决方案6】:

Swift 4.2:

controller.searchBar.layer.borderWidth = 1
controller.searchBar.layer.borderColor = UIColor(red: 255/255, green: 253/255, blue: 247/255, alpha: 1.0).cgColor

根据 Ayush 的回答回答。

【讨论】:

    【解决方案7】:

    对于这条 1 px 的小线,不得不费尽周折,这太荒谬了。我已经在谷歌上搜索了几个小时来摆脱它。我尝试了不同答案的组合以使其正常工作。当我回到这里时,我意识到 Oxcug 已经有了它,但它是在 Objective-C 中的,这对我来说不是原生的。

    无论如何,这就是 Swift 5 中的答案。如果您想在实际搜索文本字段中添加颜色背景,我也添加了。

    // these 2 lines get rid of the 1 px line
    searchBar.backgroundColor = .white
    searchBar.backgroundImage = UIImage()
    
    // this line will let you color the searchBar textField where the user actually types
    searchBar.searchTextField.backgroundColor = UIColor.lightGray
    

    【讨论】:

      【解决方案8】:

      在将UISearchBar 放在那里之前,将 tableHeaderView 设置为 nil

      如果这没有帮助,请尝试掩盖它。首先将您的搜索栏作为子视图添加到通用且大小合适的UIView(例如“包装器”)中,然后

      CGRect frame = wrapper.frame;
      CGRect lineFrame = CGRectMake(0,frame.size.height-1,frame.size.width, 1);
      UIView *line = [[UIView alloc] initWithFrame:lineFrame];
      line.backgroundColor = [UIColor whiteColor]; // or whatever your background is
      [wrapper addSubView:line];
      [line release];
      

      然后将其添加到tableHeaderView中。

      self.tableView.tableHeaderView = wrapper;
      [wrapper release];
      

      【讨论】:

      • 感谢您的解决方案。不幸的是,这两种解决方案都没有覆盖这条线。也许它是表格视图顶部的阴影?奇怪!
      • "将tableHeaderView 设置为nil,然后再将UISearchBar 放在那里..." 为什么这样会起作用?
      【解决方案9】:

      这个问题已经解决了,但也许我的解决方案可以帮助其他人。我有一个类似的问题,除了我试图删除 1px 顶部边框。

      如果你继承UISearchBar,你可以像这样覆盖框架。

      - (void)setFrame:(CGRect)frame {
          frame.origin.y = -1.0f;
          [super setFrame:frame];
      
          self.clipsToBounds = YES;
          self.searchFieldBackgroundPositionAdjustment = UIOffsetMake(0, 1.0f);
      }
      

      或者如果你想修复底部像素,你可以做这样的事情,(未经测试)。

      - (void)setFrame:(CGRect)frame {
          frame.origin.y = 1.0f;
          [super setFrame:frame];
      
          self.clipsToBounds = YES;
          self.searchFieldBackgroundPositionAdjustment = UIOffsetMake(0, -1.0f);
      }
      

      为了简单起见,setFrame 中的 clipsToBoundssearchFieldBackgroundPositionAdjustment

      另外,searchFieldBackgroundPositionAdjustment 只需要重新居中搜索字段。

      更新

      事实证明,当 searchBar 处于活动状态时,tableView 将从更新 origin.y 偏移 1px。感觉有点奇怪。我意识到解决方案就像设置一样简单,self.clipsToBounds = YES;

      【讨论】:

        【解决方案10】:

        我用过

        import UIKit
        
        class ViewController: UIViewController {
            
            @IBOutlet weak var searchBar: UISearchBar!
            
        
        
            override func viewDidLoad() {
                super.viewDidLoad()
                searchBar.backgroundImage = UIImage() // Removes the line
        

        它就像一个魅力:)

        【讨论】:

          【解决方案11】:

          警告。这是一个黑客。想知道一个更好,更正式的方式。

          您可以使用小马调试器来确定它在子视图层次结构中的位置。我认为您看到的是一个名为“分隔符”的私有 UIImageView

          - (void)viewWillAppear:(BOOL)animated
          {
            [super viewWillAppear:animated];
            for (UIView* view in self.searchBar.subviews)  {
              if (view.frame.size.height == 1 && [view isKindOfClass:[UIImageView class]]) {
                view.alpha = 0;
                break;
              }
            } 
          }
          

          【讨论】:

            【解决方案12】:

            你可以使用

              [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"someImage.png"]forState:UIControlStateNormal];
            

            在 iOS 5+ 上摆脱这条线。

            【讨论】:

            • 这会隐藏搜索文本输入字段而不是栏的底线。
            【解决方案13】:

            与我合作是,将搜索栏 barTintColor 设置为导航栏颜色

                searchBar.barTintColor = UIColor.colorWithHexString(hexStr: "479F46")
            

            经过测试,iOS 10.x 和 11.x 都解决了这个问题

            GIF:

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2023-03-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多