Customize search bar

前端 未结 5 707
花落未央
花落未央 2020-12-20 06:51

I am designing a search bar. I need the search bar to look like one in the image. Tried few ways but no changes. Help much appreciated.

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 07:22

    self.searchBar is an IBOutlet. You can also create it dynamically.

    self.searchBar.layer.borderWidth = 2.0;
    self.searchBar.layer.borderColor = [UIColor brownColor].CGColor;
    self.searchBar.layer.cornerRadius = 15.0;
    self.searchBar.barTintColor = [UIColor colorWithRed:255/255.0 green:246/255.0 blue:241/255.0 alpha:1.0];
    self.searchBar.backgroundColor = [UIColor clearColor];
    
    UITextField *textField = [self.searchBar valueForKey:@"_searchField"];
    textField.textColor = [UIColor brownColor];
    textField.placeholder = @"Search";
    textField.leftViewMode = UITextFieldViewModeNever; //hiding left view
    textField.backgroundColor = [UIColor colorWithRed:255/255.0 green:246/255.0 blue:241/255.0 alpha:1.0];
    textField.font = [UIFont systemFontOfSize:18.0];
    [textField setValue:[UIColor brownColor] forKeyPath:@"_placeholderLabel.textColor"];
    
    UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];
    imgview.image = [UIImage imageNamed:@"searchIcon.png"]; //you need to set search icon for textfield's righ view
    
    textField.rightView = imgview;
    textField.rightViewMode = UITextFieldViewModeAlways;
    

    Output :

提交回复
热议问题