Search code examples
objective-cuitextfielduisearchbarrounded-corners

Search bar rounded corner


How to change the border of search bar to rounded corner. Below is my code and I need the orange coloured border to be rounded not as sharp rectangle. Please help

// Search bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 60, 270, 41)];
self.searchBar.delegate = self;
self.searchBar.placeholder = MEGLocalizedString(@"search_hint_home_screen", nil);
UIColor *searchBarBackgroundColor = [UIColor clearColor];
self.searchBar.backgroundImage = [UIImage imageFromColor:searchBarBackgroundColor];
[self.searchBar setImage:[[UIImage imageNamed:@"search_icon_general.png"] imageTintedWithColor:primaryEventColor] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    UIView *textField = [self.searchBar subviewWithKindOfClass:[UITextField class]];
    textField.backgroundColor = [UIColor orangeColor];
//        textField.layer.borderWidth = 1;
    textField.layer.cornerRadius = 14;
    [(UITextField *)textField setBorderStyle:UITextBorderStyleNone];
    // Change the search bar placeholder text color
    [textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
}
self.searchBar.layer.cornerRadius = 14;
self.searchBar.layer.borderColor = [UIColor orangeColor].CGColor;

enter image description here


Solution

  • This solve your problem

    textField.layer.masksToBounds = true