Make UISearchBar background clear

后端 未结 17 1221
野的像风
野的像风 2021-01-07 17:17

I am not able to clear search bar I have tried to make it clear by setting its background color clear and I have also placed one image under searchbar

I have also ma

17条回答
  •  天涯浪人
    2021-01-07 17:40

    extension UISearchBar {
        func changeSearchBarColor(fieldColor: UIColor, backColor: UIColor, borderColor: UIColor?) {
            UIGraphicsBeginImageContext(bounds.size)
            backColor.setFill()
            UIBezierPath(rect: bounds).fill()
            setBackgroundImage(UIGraphicsGetImageFromCurrentImageContext()!, for: UIBarPosition.any, barMetrics: .default)
    
            let newBounds = bounds.insetBy(dx: 0, dy: 8)
            fieldColor.setFill()
            let path = UIBezierPath(roundedRect: newBounds, cornerRadius: newBounds.height / 2)
    
            if let borderColor = borderColor {
                borderColor.setStroke()
                path.lineWidth = 1 / UIScreen.main.scale
                path.stroke()
            }
    
            path.fill()
            setSearchFieldBackgroundImage(UIGraphicsGetImageFromCurrentImageContext()!, for: UIControlState.normal)
    
            UIGraphicsEndImageContext()
        }
    }
    

提交回复
热议问题