Iphone 在iOS5中隐藏UISearchBar背景

Iphone 在iOS5中隐藏UISearchBar背景,iphone,ios4,Iphone,Ios4,下面隐藏UIsearchBar背景的代码在iOs4.2之前可以正常工作,但在iOs4.3或更高版本中不能正常工作 for (UIView *subview in searchBar.subviews) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { [subview removeFromSuperview]; break; } } 这段代码没

下面隐藏
UIsearchBar
背景的代码在iOs4.2之前可以正常工作,但在iOs4.3或更高版本中不能正常工作

for (UIView *subview in searchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
}

这段代码没有问题……但也给了您另一种解决方案,将for循环的代码替换为to

    [[[searchBar subviews] objectAtIndex:0] removeFromSuperview];

这段代码没有问题……但也给了您另一种解决方案,将for循环的代码替换为to

    [[[searchBar subviews] objectAtIndex:0] removeFromSuperview];

我知道很晚了,但这里有参考

调度异步(调度获取全局队列(调度队列优先级默认为0)^{ CGRect rect=CGRectMake(0.0f,0.0f,1.0f,1.0f); UIGraphicsBeginImageContext(矩形大小); UIImage*image=nil; CGContextRef context=UIGraphicsGetCurrentContext(); 如果(上下文){ CGContextSetFillColorWithColor(上下文[UIColor clearColor].CGColor); CGContextFillRect(上下文,rect); image=UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsSendImageContext()


我知道很晚了,但这里有参考

调度异步(调度获取全局队列(调度队列优先级默认为0)^{ CGRect rect=CGRectMake(0.0f,0.0f,1.0f,1.0f); UIGraphicsBeginImageContext(矩形大小); UIImage*image=nil; CGContextRef context=UIGraphicsGetCurrentContext(); 如果(上下文){ CGContextSetFillColorWithColor(上下文[UIColor clearColor].CGColor); CGContextFillRect(上下文,rect); image=UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsSendImageContext()


在iOS 7中,向UISearchBar添加了另一个UIView,以下是iOS 7的解决方案:

UIView *vw = [yourSearchBar.subviews objectAtIndex:0];
for (id img in vw.subviews) {
    if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [img removeFromSuperview];
    }
}
您还可以执行以下操作(也适用于iOS 7):


在iOS 7中,向UISearchBar添加了另一个UIView,以下是iOS 7的解决方案:

UIView *vw = [yourSearchBar.subviews objectAtIndex:0];
for (id img in vw.subviews) {
    if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [img removeFromSuperview];
    }
}
您还可以执行以下操作(也适用于iOS 7):


由于外观API,视图层次结构可能已更改。请在
-viewDidLoad
中设置断点,并在程序暂停时键入
po[searchBar recursiveDescription]
在控制台中获取整个视图层次结构的列表,包括私有类名。@马克·亚当斯-不知道,但通过nib编程创建UIsearchBar对我来说很有用。如果您在
-initWithFrame:
中执行代码,那么这一点很明显。从nib加载时使用
-initWithCoder:
初始值设定项。@Mark-我正在viewDidLoad中执行代码。在这种情况下,
searchBar
可能为零,因为它的出口未连接到界面生成器。视图层次结构可能因外观API而发生更改。在
-viewDidLoad
中设置断点,在程序暂停时,键入
po[searchBar recursiveDescription]
在控制台中获取整个视图层次结构的列表,包括私有类名。@马克·亚当斯-不知道,但通过nib编程创建UIsearchBar对我来说很有用。如果您在
-initWithFrame:
中执行代码,那么这一点很明显。从nib加载时使用
-initWithCoder:
初始值设定项。@Mark-我正在viewDidLoad中执行代码。在这种情况下,
searchBar
可能为零,因为它的插座没有连接到接口生成器。