iOS 設置自帶UISearchBar的背景顏色(並改為圓角)


 

 

/** 取消searchBar背景色 */

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size

{

    CGRect rect = CGRectMake(0, 0, size.width, size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

      return image;

}

 

然后在viewdidload里面

/** 設置背景圖片 */

    [_searchBar setBackgroundImage:[UIImage imageNamed:@"red_nav_bar"]];   //這里圖片的顏色和導航欄一樣

/** 設置背景色 */

   [_searchBar setBackgroundColor:[UIColor redColor]];

/** 設置文本框背景 * 

[_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"imageView"] forState:UIControlStateNormal];

 

 

    //更改search圓角

        UITextField *searchField = [self.searchBar valueForKey:@"searchField"];

        if (searchField) {

            [searchField setBackgroundColor:[UIColor whiteColor]];

            searchField.layer.cornerRadius = 14.0f;

            searchField.layer.borderColor = [UIColor colorWithRed:247/255.0 green:75/255.0 blue:31/255.0 alpha:1].CGColor;

            searchField.layer.borderWidth = 1;

            searchField.layer.masksToBounds = YES;

        }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM