在导航栏添加搜索框

时间:2025-03-14 07:48:44
     //导航条的搜索条
         UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(500.0f,0.0f,250.0f,44.0f)];
          = self;
         [searchBar setPlaceholder:@ "搜索" ];
         
         //将搜索条放在一个UIView上
         UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 44)];
          = [UIColor clearColor];
         [searchView addSubview:searchBar];
         
          = searchView;
      
// 自定义导航栏的背景:
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];   //@{}代表Dictionary   
  [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];