//思路:正常SearchBar有一個灰色環繞邊框,在searchBar外層加一個白色的背景,這個背景只夠包圍searchBar的搜索框部分 //同時調節searchBar的frame讓它正好在白色背景呢,這樣灰色的邊框就在背景外面不能被顯示出來了 UIView *searchBGView = [[UIView alloc] initWithFrame:CGRectMake(8, ORIGIN_Y + 7, DEVICE_W - 8 * 2, 28)]; searchBGView.backgroundColor = [UIColor whiteColor]; searchBGView.layer.borderColor = [UIColor colorWithRed:242/255.0f green:242/255.0f blue:242/255.0f alpha:1].CGColor; searchBGView.layer.borderWidth = 0.5f; searchBGView.layer.masksToBounds = YES; searchBGView.layer.cornerRadius = 4; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-10, 0, searchBGView.frame.size.width + 20, searchBGView.frame.size.height)]; searchBar.delegate = self; searchBar.backgroundColor = C_white; searchBar.returnKeyType = UIReturnKeySearch; searchBar.showsCancelButton = NO; searchBar.showsSearchResultsButton = NO; searchBar.placeholder = @"點擊搜索品牌"; [self.view addSubview:searchBGView]; [searchBGView addSubview:searchBar];