ios 自定义 UISearchBar:尝试去掉搜索栏下方的 1px 黑线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7620564/
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
Customize UISearchBar: Trying to get rid of the 1px black line underneath the search bar
提问by strave
My question is already specified in the title: I would like to get rid of the black line drawn on the bottom of the UISearchBar
. Any ideas?
我的问题已经在标题中说明了:我想去掉UISearchBar
. 有任何想法吗?
Here's an image of what I mean:
这是我的意思的图像:
UPDATE:
更新:
I think that the line is part of the UITableView
's tableHeaderView
. I still don't know how to remove it.
我认为该行是UITableView
's 的一部分tableHeaderView
。我仍然不知道如何删除它。
采纳答案by Nonlinearsound
I fixed this by adding a subview
to the searchBar
's view stack like so:
我通过向的视图堆栈添加 asubview
来解决这个问题,searchBar
如下所示:
CGRect rect = self.searchBar.frame;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, rect.size.height-2,rect.size.width, 2)];
lineView.backgroundColor = [UIColor whiteColor];
[self.searchBar addSubview:lineView];
Here, self.searchBar
is an UISearchBar
pointer of my controller class.
这里,self.searchBar
是UISearchBar
我的控制器类的指针。
回答by Ayush Goel
Try this
尝试这个
searchBar.layer.borderWidth = 1;
searchBar.layer.borderColor = [[UIColor lightGrayColor] CGColor];
回答by Oxcug
Why:
为什么:
So, I've dug into the API's trying to figure out why this is happening. Apparently whomever wrote the UISearchBar
API is rasterizing the lines onto an image and setting it as it's backgroundImage
.
所以,我深入研究了 API,试图找出发生这种情况的原因。显然,编写UISearchBar
API 的人正在将线条光栅化到图像上并将其设置为backgroundImage
.
Solution:
解决方案:
I propose a simpler solution, if you want to set the backgroundColor
and get rid of the hairlines:
我提出了一个更简单的解决方案,如果你想设置backgroundColor
和摆脱细线:
searchBar.backgroundColor = <#... some color #>
searchBar.backgroundImage = [UIImage new];
Or if you just need a background image without the hairlines:
或者,如果您只需要没有细线的背景图像:
searchBar.backgroundImage = <#... some image #>
回答by ancajic
I have 0.5px
black horizontal lines both on top and on the bottom of my UISearchBar
. The only way I had so far to get rid of them is by setting its style to Minimal
:
我的0.5px
顶部和底部都有黑色水平线UISearchBar
。到目前为止,我摆脱它们的唯一方法是将其样式设置为Minimal
:
mySearchBar.searchBarStyle = UISearchBarStyleMinimal;
回答by VBaarathi
Swift 4.2:
斯威夫特 4.2:
controller.searchBar.layer.borderWidth = 1
controller.searchBar.layer.borderColor = UIColor(red: 255/255, green: 253/255, blue: 247/255, alpha: 1.0).cgColor
Answer based on Ayush's answer.
根据 Ayush 的回答回答。
回答by cnotethegr8
This question has already been solved but maybe my solution can help someone else. I had a similar problem, except I was trying to remove the 1px top border.
这个问题已经解决了,但也许我的解决方案可以帮助别人。我有一个类似的问题,除了我试图删除 1px 顶部边框。
If you subclass UISearchBar
you can override the frame like this.
如果你子类化,UISearchBar
你可以像这样覆盖框架。
- (void)setFrame:(CGRect)frame {
frame.origin.y = -1.0f;
[super setFrame:frame];
self.clipsToBounds = YES;
self.searchFieldBackgroundPositionAdjustment = UIOffsetMake(0, 1.0f);
}
Or if you would like to fix the bottom pixel you could do something like this, (untested).
或者如果你想修复底部像素,你可以做这样的事情,(未经测试)。
- (void)setFrame:(CGRect)frame {
frame.origin.y = 1.0f;
[super setFrame:frame];
self.clipsToBounds = YES;
self.searchFieldBackgroundPositionAdjustment = UIOffsetMake(0, -1.0f);
}
Only for simplicity of the example are the clipsToBounds
and searchFieldBackgroundPositionAdjustment
in the setFrame
.
仅适用于例如简单是clipsToBounds
和searchFieldBackgroundPositionAdjustment
中setFrame
。
Also the searchFieldBackgroundPositionAdjustment
is only needed to re-center the search field.
也searchFieldBackgroundPositionAdjustment
只需要重新居中搜索字段。
UPDATE
更新
It turns out that the tableView
will shift 1px from updating the origin.y
while the searchBar is active. It feels a little strange. I realized that the solution is as simple as setting, self.clipsToBounds = YES;
事实证明,当 searchBar 处于活动状态时tableView
,更新将移动 1px origin.y
。感觉有点奇怪。我意识到解决方案就像设置一样简单,self.clipsToBounds = YES;
回答by Mundi
Set the tableHeaderView to nil
before putting your UISearchBar
there.
将 tableHeaderView 设置为nil
在放置之前UISearchBar
。
If that does not help, try to cover it up. First add your search bar to a generic and appropriately sized UIView
(say, "wrapper") as a subview, then
如果这没有帮助,请尝试掩盖它。首先将您的搜索栏UIView
作为子视图添加到通用且适当大小的(例如“包装器”)中,然后
CGRect frame = wrapper.frame;
CGRect lineFrame = CGRectMake(0,frame.size.height-1,frame.size.width, 1);
UIView *line = [[UIView alloc] initWithFrame:lineFrame];
line.backgroundColor = [UIColor whiteColor]; // or whatever your background is
[wrapper addSubView:line];
[line release];
And then add it to the tableHeaderView.
然后将其添加到tableHeaderView。
self.tableView.tableHeaderView = wrapper;
[wrapper release];
回答by Ray Fix
Warning. This is a hack. Would like to know a better, more official way.
警告。这是一个黑客。想知道一个更好的,更正式的方法。
You can use the pony debugger to figure out where in the subview hierarchy it is. I think the thing you are see is a private UIImageView called "separator"
您可以使用 pony 调试器找出它在子视图层次结构中的位置。我认为您看到的是一个名为“分隔符”的私有 UIImageView
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
for (UIView* view in self.searchBar.subviews) {
if (view.frame.size.height == 1 && [view isKindOfClass:[UIImageView class]]) {
view.alpha = 0;
break;
}
}
}
回答by Andrew
You can use
您可以使用
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"someImage.png"]forState:UIControlStateNormal];
on iOS 5+ to get rid of the line.
在 iOS 5+ 上摆脱这条线。