ios - 删除 UISearchBar 的上下边框和阴影?

标签 ios swift

我创建了一个带有 UISearchBar 的 .xib。

我希望删除顶部和底部边框,如下图所示:image

顶部边框下方还有一个看起来像阴影的东西。

我尝试过更改背景/色调等,但很难让它消失。

欢迎任何帮助,有什么想法吗?

编辑

所以下面的代码允许我去掉顶部和底部的边框,但不能去掉奇怪的阴影。我添加了绿色背景以更清楚地显示它。

See

此外,在文本框(而不是 View )周围添加边框的最佳方法是什么?谢谢

//** 这就是我想要做的:https://stackoverflow.com/questions/33107058/uiviewcontroller-sizing-as-maincontroller-within-uisplitviewcontroller

这是我正在使用的代码。

import UIKit

class TopSearchViewController: UIView {

    var expanded: Int = 0

    @IBOutlet weak var trailingConstraint: NSLayoutConstraint!
    @IBOutlet var contentView: UIView!
    @IBOutlet weak var searchBar: UISearchBar!

    @IBAction func advancedSearchButton(sender: AnyObject) {
        if expanded == 0 {
            self.layoutIfNeeded()
            UIView.animateWithDuration(0.1, animations: {
                self.trailingConstraint.constant = 200
                self.layoutIfNeeded()
            })
            expanded = 1
        } else {
            self.layoutIfNeeded()
            UIView.animateWithDuration(0.1, animations: {
                self.trailingConstraint.constant = 25
                self.layoutIfNeeded()
            })
            expanded = 0
        }
    }

    override init(frame: CGRect) { // for using CustomView in code
        super.init(frame: frame)
        self.commonInit()
    }

    required init(coder aDecoder: NSCoder) { // for using CustomView in IB
        super.init(coder: aDecoder)
        self.commonInit()
    }

    private func commonInit() {
        NSBundle.mainBundle().loadNibNamed("TopSearchViewController", owner: self, options: nil)

        contentView.frame = self.bounds
        contentView.autoresizingMask = .FlexibleHeight | .FlexibleWidth

        self.addSubview(contentView)

        self.searchBar.layer.borderWidth = 1
        self.searchBar.layer.borderColor = UIColor.whiteColor().CGColor

        println(searchBar)

        for subview in searchBar.subviews {
            println("hello")

            var textField : UITextField

            if (subview.isKindOfClass(UITextField)) {
                textField = subview as! UITextField
                textField.borderStyle = .None
                textField.layer.borderWidth = 1
                textField.layer.borderColor = UIColor.lightGrayColor().CGColor
                textField.layer.cornerRadius = 14
                textField.background = nil;
                textField.backgroundColor = UIColor.whiteColor()
            }
        }

    }

    func viewDidLoad() {

    }
}

最佳答案

将栏的 backgroundImage 设置为 empty UIImagenot nil:

searchBar.backgroundImage = UIImage()

您还可以设置 barTintColor,正如您在下面的评论中指定的那样。

关于ios - 删除 UISearchBar 的上下边框和阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33146848/

相关文章:

ios - 如何为多个类 Swift 进行扩展

ios - 单击后退按钮返回 2 segue

ios - 当我想使用 tableview.deleteRows(at : , with: ) 时应用程序崩溃

ios - 导出或提交到 App Store 时 Xcode 崩溃

ios - UICollectionView 在 Xcode 6.1 中崩溃 在 Xcode 5.1.1 中工作正常

iphone - 为什么页面在我的 iOS 上的 jQuery Mobile PhoneGap 应用程序中转换后会闪烁?

Swift - 将参数传递给 Web 服务

iphone - 如何读取屏幕以查看哪些像素是白色的

ios - GPUImage内存错误

ios - 为什么 Swift 可选变成零?