Remove border between View and Search Bar

前端 未结 7 834
傲寒
傲寒 2020-12-17 14:54

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

Note how the status bar is the same color as the s

7条回答
  •  不思量自难忘°
    2020-12-17 15:24

    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.

    • put UIVisualEffectView on self.view (view of your VC)
    • make custom class of searchBar, which background is transparent
    • (also let statusBar transparent)

    (swift4 code)

        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)

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

提交回复
热议问题