xcode 删除视图和搜索栏之间的边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31631567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:32:59  来源:igfitidea点击:

Remove border between View and Search Bar

iosxcodeswift

提问by Idris

So in Xcode I'm trying to create a seamless search bar. So I'm trying to replicate something like this

所以在 Xcode 中,我试图创建一个无缝的搜索栏。所以我试图复制这样的东西

enter image description here

在此处输入图片说明

Note how the status bar is the same color as the search bar. Now here's the result to my approach.

请注意状态栏如何与搜索栏颜色相同。现在这是我的方法的结果。

enter image description here

在此处输入图片说明

What I did was add a View to cover up the default status bar with the blue background. Then I added a search bar and changed it's background to blue. For some reason I end up getting a black border between the two, which ruins the "seamless" design. Any ideas on how I can remove the black border in Swift?

我所做的是添加一个视图来覆盖蓝色背景的默认状态栏。然后我添加了一个搜索栏并将其背景更改为蓝色。出于某种原因,我最终在两者之间得到了一个黑色边框,这破坏了“无缝”设计。关于如何在 Swift 中删除黑色边框的任何想法?

回答by Oxcug

For iOS 7+:

对于 iOS 7+:

searchBar.backgroundImage = UIImage()

Otherwise this will work on all iOS versions:

否则这将适用于所有 iOS 版本:

searchBar.layer.borderWidth = 1
searchBar.layer.borderColor = thatBlueColor.CGColor

回答by Robac

In Xcode 8.3 and Swift 3

在 Xcode 8.3 和 Swift 3 中

  1. Create an outlet from your search bar to your ViewController (I called mine searchBarOutlet for this example).

  2. Below viewDidLoad insert the following.

    self.searchBarOutlet.backgroundImage = UIImage()

  1. 创建一个从您的搜索栏到您的 ViewController 的出口(在这个例子中我称之为我的 searchBarOutlet)。

  2. 在 viewDidLoad 下面插入以下内容。

    self.searchBarOutlet.backgroundImage = UIImage()

You should have the following:

您应该具备以下条件:

    override func viewDidLoad() {
    super.viewDidLoad()

         self.searchBarOutlet.backgroundImage = UIImage()

When you run your app the lines will be gone (they will still be visible on storyboard).

当您运行您的应用程序时,线条将消失(它们仍将在故事板上可见)。

回答by Eduardo Amaral

In my case, beyond the edge of search bar needed to take the edge off also the navigation bar.

在我的情况下,超出搜索栏的边缘还需要去除导航栏的边缘。

C# code:

C#代码:

NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.Default);

Swift code:

SWIFT代码:

self.navigationController.navigationBar.shadowImage = UIImage()
self.navigationController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)

回答by KJ Newtown

The best solution to remove top and bottom default borders is:

删除顶部和底部默认边框的最佳解决方案是:

To set a new empty searchBar background layout in viewDidLoadfor example:

例如,要设置一个新的空 searchBar 背景布局viewDidLoad

searchBar.backgroundImage = UIImage()

回答by Vivek

Swift 4

斯威夫特 4

searchBar.barTintColor = UIColor.white
searchBar.setBackgroundImage(UIImage.init(), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)

Sample image

示例图像

enter image description here

在此处输入图片说明

Upate Sample code for navigation bar and search bar background color:

Upate 导航栏和搜索栏背景颜色的示例代码:

Navigation bar color

导航栏颜色

self.navigationController?.navigationBar.barTintColor = .blue

Search bar color

搜索栏颜色

searchBarProperty.backgroundColor = self.navigationController?.navigationBar.barTintColor

Note : Navigation bar and search bar color must be same.

注意:导航栏和搜索栏颜色必须相同。

Sample image with blue navigation bar and blue search bar enter image description here

带有蓝色导航栏和蓝色搜索栏的示例图片 在此处输入图片说明

回答by Jacobo Koenig

I found these answers to be more complicated than they needed to be. You can just modify the constraint that is binding the searchBar view and the other view to -1pts so that it overlaps exactly by the height of the searchBar's margin.

我发现这些答案比他们需要的更复杂。您只需修改将 searchBar 视图和另一个视图绑定到 -1pts 的约束,使其与 searchBar 边距的高度完全重叠。

回答by simotunes

I encountered the same situation when I set the statusBar and searchBar translucent. In this situation, I couldn't resolve with the answers written here however I could solve by the following approach.

我在设置statusBar和searchBar半透明的时候遇到了同样的情况。在这种情况下,我无法用这里写的答案解决,但是我可以通过以下方法解决。

  • put UIVisualEffectView on self.view (view of your VC)
  • make custom class of searchBar, which background is transparent
  • (also let statusBar transparent)
  • 将 UIVisualEffectView 放在 self.view 上(您的 VC 视图)
  • 制作searchBar的自定义类,背景是透明的
  • (也让 statusBar 透明)

(swift4 code)

(swift4 代码)

    class TransparentSearchBar: UISearchBar {
        override init(frame: CGRect) {
            super.init(frame: frame)
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        override func layoutSubviews() {
            super.layoutSubviews()
            makeTransparentBackground()
        }

        private func makeTransparentBackground() {
            for view in self.subviews {
                view.backgroundColor = UIColor.clear
                for subview in view.subviews {
                    if let imageview = subview as? UIImageView {
                        imageview.image = nil
                    }
                }
            }
        }

    }

somewhere in viewDidLoad (statusBar)

viewDidLoad (statusBar) 中的某处

    let statusWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
    let statusBar = statusWindow.subviews[0] as UIView
    statusBar.backgroundColor = UIColor.clear