2016-09-30 84 views
0

我想添加漸變UISearchController不起作用。UISearchController add CAGradientLayer

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]; 
+0

,你在viewDidLoad中添加此代碼,否則,如果它是在viewDidLoad中改變這viewdidappear –

+0

參考這個鏈接,HTTP://stackoverflow.com/questions/23074539/programmatically-create-a-uiview-與色梯度。 @Luis Teodoro Junior – KAR

+0

漸變是正確的並且工作正常,問題在於UISearchController無法識別漸變。 –

回答

0

問題解決了,添加該字段和運行

[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal]; 
0

您需要設置將backgroundImage到搜索欄。

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; 
}