删除UISearchBar和阴影的顶部和底部边框? [英] Remove top and bottom border of UISearchBar and shadow?
问题描述
我创建了一个带有UISearchBar的.xib。
I have created a .xib with a UISearchBar.
我想删除顶部和底部边框,如下图所示:
I am looking to remove the top and bottom borders as seen in this image:
在顶部边框下面还有一个阴影。
There is also what looks like a shadow underneath the top border.
我试过改变背景/色彩等等,但努力让它消失。
I have tried changing backgrounds/tint colours etc, but struggling to get it to disappear.
欢迎任何帮助,任何想法?
Any help would be welcome, any ideas?
编辑
所以下面的代码可以让我摆脱顶部和底部的边框,但不是奇怪的投影。我添加了绿色背景以更清楚地显示它。
So the code below allows me to get rid of the border at the top and bottom, but not the weird drop shadow. I've added green background to show it more clearly.
另外,在文本框(而不是视图)周围添加边框的最佳方法是什么?谢谢
Also, what would be the best way to add a border around the text box (rather than the view)?? Thanks
// **这就是我要做的: https://stackoverflow.com/questions/33107058/uiviewcontroller-sizing-as-maincontroller-within-uisplitviewcontroller
//** This is what I'm trying to do: https://stackoverflow.com/questions/33107058/uiviewcontroller-sizing-as-maincontroller-within-uisplitviewcontroller
这是我正在使用的代码。
and here is the code I am using.
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() {
}
}
推荐答案
设置bar的 backgroundImage
到空 UIImage
,不 nil
:
Set the bar's backgroundImage
to an empty UIImage
, not nil
:
searchBar.backgroundImage = UIImage()
您也可以设置 barTintColor
,如下面评论中所指定的那样。
You may also set the barTintColor
, as you specified in your comment below.
这篇关于删除UISearchBar和阴影的顶部和底部边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!