Uisearchbar UI搜索栏范围栏颜色
如果有人设法为UISearchBar的scopebar部分着色,则该部分具有tintColor属性,但设置该属性不会影响附加的scopebar UISegmentedControl。我在吧台上有一个把手,可以给它上色,但似乎不起作用Uisearchbar UI搜索栏范围栏颜色,uisearchbar,Uisearchbar,如果有人设法为UISearchBar的scopebar部分着色,则该部分具有tintColor属性,但设置该属性不会影响附加的scopebar UISegmentedControl。我在吧台上有一个把手,可以给它上色,但似乎不起作用 for (id subview in searchBar.subviews){ if([subview isMemberOfClass:[UISegmentedControl class]]){ UISegmentedControl *scopeBar
for (id subview in searchBar.subviews){
if([subview isMemberOfClass:[UISegmentedControl class]]){
UISegmentedControl *scopeBar=(UISegmentedControl *) subview;
scopeBar.tintColor=UIColorFromRGB(0x990066);
}
}
干杯,尼尔这很有趣,范围栏是一个定制的分段控件 从docs:UISegmentedControl中,仅当分段控件的样式为UISegmentedControlStyleBar时,UISegmentedControl才使用此属性 现在,范围栏分段控件看起来像UISegmentedControlStyleBar,但不是,它是一些未记录的样式:
NSLog(@"scope bar style is %d", scopeBar.segmentedControlStyle);
> scope bar style is 7
你可以试试这个,它确实设置了色调,但看起来像屁股:
[scopeBar setSegmentedControlStyle:UISegmentedControlStyleBar];
[scopeBar setTintColor: UIColorFromRGB(0x990066)];
> scope bar style is 2
内部有一些实例变量会影响这一点:_segementedControlFlags.style和_barStyle,但除非绕过受认可的API,否则无法破解它们
最好的做法是向苹果公司提出,并希望他们在未来的版本中包括一个修复程序。我无法可靠地为范围栏设置色调
我最后做的是更改搜索栏的tintColor
。。。在转换到搜索模式之前,我将其设置为与范围栏匹配的默认颜色。然后,当取消搜索时,我将其更改回
这里我似乎缺少的一点是当点击相应的tableView
时发生的转换,导致搜索栏失去第一响应者状态。在这种情况下,颜色不会变回原来的颜色。原因:我把颜色的改变建立在点击搜索栏的“取消”按钮上
下意识的反应:
searchBarCancelButtonClicked:
应该被调用(我们在其中更改回色调)。。。但这不可能是对的,因为没有单击“取消”。我真的需要一个“搜索完成”类型的检测。也许只需关注第一响应者,看看ui搜索栏何时进入和退出该状态?以下是一些复制粘贴友好的代码,供其他人讨论
scopeBar.segmentedControlStyle = UISegmentedControlStyleBar; //required for color change
for (id subview in self.searchDisplayController.searchBar.subviews )
{
if([subview isMemberOfClass:[UISegmentedControl class]])
{
UISegmentedControl *scopeBar=(UISegmentedControl *) subview;
scopeBar.tintColor = [UIColor blackColor];
}
}
如果在按钮周围有一个白色框状区域,可以通过interface builder更改该区域的颜色
我不会说它像其他人一样看起来像驴子,但也不是很理想。
这个问题的另一个创可贴解决方案(一个不那么复杂的)。您可以在搜索栏和范围栏的顶部添加半透明的UIImageView,并将UIImageView的背景色更改为所需的色调。
上述方法对我不适用,我最终使用了iOS外观方法[[UISegmentedControl appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
更改颜色以更改范围栏的颜色。注意BarTintColor不同-这将更改搜索栏的颜色。只需执行以下操作即可更改ScopeBar的着色颜色: 下面的代码是C#(Xamarin)
该解决方案采用Swift,可适用于objective-c。您可以使用下面的函数找到分段控件视图
func findSegmentedControl(view : UIView) -> UIView? {
for v in view.subviews {
if v is UISegmentedControl {
print(v)
return v
} else {
if let found = findSegmentedControl(view : v) {
return found
}
}
}
return nil
}
然后设置视图的着色颜色。调用此函数将如下所示:
if let segmentedView = findSegmentedControl(view: searchController.searchBar) {
segmentedView.tintColor = UIColor.white
}
不幸的是,就像你自己说的,这看起来真的像个屁眼。
if let segmentedView = findSegmentedControl(view: searchController.searchBar) {
segmentedView.tintColor = UIColor.white
}