使用UISearchBar时使其余视图变灰

Nie*_*bæk 2 xcode objective-c uisearchbar ios

我想模仿似乎是使用UISearchBar的标准UI,现在我尝试将其余视图设置为灰色,当我开始搜索时,我尝试通过将视图的背景颜色设置为灰色,但我在UITableView中使用了部分,并且它们没有变灰.任何人有任何理由为什么?

使用搜索栏时的UITableView

rav*_*ron 5

您没有看到部分标题淡入灰色的原因是因为它们是在灰色背景上绘制的,并且是不透明的; 换句话说,他们alpha是1.

如果您正在寻找获得所需效果的建议,我的第一反应是添加叠加层UIView作为您想要"变灰"的区域的子视图,并在某些事件发生时更改其背景颜色.像这样的东西:

// You have to choose what view is the one that needs to be grayed out.
// I'll call the view you want to gray out "viewToBeGrayed"

UIView *grayOverlay = [[UIView alloc] initWithFrame:viewToBeGrayed.frame];

// Initially, the overlay should be clear
[grayOverlay setBackgroundColor:[UIColor clearColor]];

[viewToBeGrayed addSubview:grayOverlay];
Run Code Online (Sandbox Code Playgroud)

然后,当您想要"灰显"时viewToBeGrayed,只需将背景颜色更改grayOverlay为某个半透明灰度值:

[grayOverlay setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
Run Code Online (Sandbox Code Playgroud)

最后,当你想摆脱的"变灰"的效果,只需设置视图回清除:

[grayOverlay setBackgroundColor:[UIColor clearColor]];
Run Code Online (Sandbox Code Playgroud)

这应该是一个很好的起点.如果您需要任何其他帮助,请评论.