iOSPawan
iOSPawan

Reputation: 2894

Hide UISearchBar background in iOS5

The below code to hide background of UIsearchBar works fine till iOs4.2 but not in iOS4.3 or later.

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

Upvotes: 0

Views: 1636

Answers (3)

RyanG
RyanG

Reputation: 4503

In iOS 7 another UIView was added to the UISearchBar, here is the solution for iOS 7:

UIView *vw = [yourSearchBar.subviews objectAtIndex:0];
for (id img in vw.subviews) {
    if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [img removeFromSuperview];
    }
}

You could also do the following (also works in iOS 7):

[yourSearchBar setBackgroundImage:[UIImage new]];
[yourSearchBar setTranslucent:YES];

Upvotes: 3

Siby
Siby

Reputation: 318

I know it is late but for reference here it is

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); UIImage *image = nil; CGContextRef context = UIGraphicsGetCurrentContext(); if (context) { CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect); image= UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsEndImageContext();

    dispatch_async(dispatch_get_main_queue(), ^{
        self.backgroundImage = image;
    });
});

Upvotes: 0

Dipak Chaudhari
Dipak Chaudhari

Reputation: 655

Their is no issue for this code ....but also giving you a alternate solution , replace code of for loop to

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

Upvotes: 1

Related Questions