0

I would like to add gradient in UISearchController not work.

enter image description here

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;

self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;



[self.searchController.searchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 79)];


CAGradientLayer * grad = [CAGradientLayer layer];
grad.frame =  self.searchController.searchBar.bounds;

grad.colors = @[(id)UIColorFromRGB(0x8daf00).CGColor,
                    (id)UIColorFromRGB(0xd1af03).CGColor];


[ self.searchController.searchBar.layer insertSublayer:grad atIndex:0];
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
  • where you add this code in viewdidload or else, if it is in viewdidload change this to viewdidappear – Anbu.Karthik Sep 30 '16 at 11:43
  • refer this link ,http://stackoverflow.com/questions/23074539/programmatically-create-a-uiview-with-color-gradient. @Luis Teodoro Junior – KAR Sep 30 '16 at 11:48
  • the gradient is correct and working, the problem is that the UISearchController not recognize the gradient. – Luis Teodoro Junior Sep 30 '16 at 11:51

2 Answers2

0

Problem solved, add this field and run

[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
0

You need to set backgroundimage to searchbar.

  gradient.locations = @[@0.0, @0.1];
  [self.searchController.searchBar setBackgroundImage:[self imageFromLayer:gradient]];


- (UIImage *)imageFromLayer:(CALayer *)layer
{
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, 0);

    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return outputImage;
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84