Подтвердить что ты не робот

Как настроить внешний вид UISearchBar

Я хочу настроить панель поиска, я имею в виду белый ящик. Могу ли я это сделать? Есть ли документация об этом? Есть ли способ скрыть белый ящик, но не буквы. Могу ли я хотя бы сделать коробку меньше? Я имею в виду меньшую высоту

4b9b3361

Ответ 1

В IOS 5.0 у вас есть возможность использовать

  [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbar.png"]forState:UIControlStateNormal];

Ответ 2

Вот решение, которое я искал: Подклассификация UISearchBar и перезапись метода layoutSubviews

- (void)layoutSubviews {
   UITextField *searchField;
   NSUInteger numViews = [self.subviews count];
   for(int i = 0; i < numViews; i++) {
      if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
        searchField = [self.subviews objectAtIndex:i];
      }
   }
   if(!(searchField == nil)) {
       searchField.textColor = [UIColor whiteColor];
       [searchField setBackground: [UIImage imageNamed:@"buscador.png"] ];
       [searchField setBorderStyle:UITextBorderStyleNone];
   }

   [super layoutSubviews];
}

Ответ 3

// Search Bar Customization
// Background Image
[self.searchDisplayController.searchBar setBackgroundImage:[[UIImage alloc]init]];

// Backgroud Color
[self.searchDisplayController.searchBar setBackgroundColor:[UIColor redColor]];

// Search Bar Cancel Button Color
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];

// set Search Bar Search icon
[self.searchDisplayController.searchBar setImage:[UIImage imageNamed:@"search_ico.png"]
                                forSearchBarIcon:UISearchBarIconSearch
                                           state:UIControlStateNormal];

// set Search Bar textfield background image
 [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_box.png"]
                                                forState:UIControlStateNormal];

// set Search Bar texfield corder radius
UITextField *txfSearchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
txfSearchField.layer.cornerRadius = 10.8f;

Ответ 4

Вот некоторые общие свойства, которые вы можете изменить для настройки UISearchBar: введите описание изображения здесь

Ответ 6

Там мало попыток, включая подкласс UISearchBar и взломать его layoutSubviews или drawLayer:. После iOS 5 лучшим методом является использование UIAppearance.

// Configure your images
UIImage *backgroundImage = [UIImage imageNamed:@"searchbar"];
UIImage *searchFieldImage = [[UIImage imageNamed:@"searchfield"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

// Set it to your UISearchBar appearance
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];

Ответ 7

используйте нижеприведенный код для прозрачной панели поиска

// Set it to your UISearchBar appearance

[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage 
                                                forState:UIControlStateNormal];

Ответ 8

Некоторые примеры в Swift

UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
        UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
        UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()

  UISearchBar.appearance().setImage(UIImage(named: "searchBarSearchIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
        UISearchBar.appearance().setImage(UIImage(), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)