1. 程式人生 > >iOS UISearchBar的背景顏色修改

iOS UISearchBar的背景顏色修改

測試環境 :iOS 9.3

修改內部輸入框的顏色為紅色 ,背景顏色為綠色

UISearchBar * search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 250,320, 44)];
    
    [self.view addSubview:search];
    
    // 經測試, 需要設定barTintColor後, 才能拿到UISearchBarTextField物件
    search.barTintColor = [UIColor greenColor];//這裡修改的searchBar的背景顏色
    UITextField * searchTextField = [[[search.subviews firstObject] subviews] lastObject];

   // search.backgroundColor = [UIColor greenColor];
    [searchTextField setBackgroundColor:[UIColor redColor]];