首页 > 代码库 > iOS---searchBar 搜索框 光标初始位置后移

iOS---searchBar 搜索框 光标初始位置后移

  1. #import <UIKit/UIKit.h>
  2. @interface SearchBar : UITextField
  3. @property (nonatomic,strong) UIButton *button;
  4. + (instancetype)searchBar;
  5. @end
  6. #import "SearchBar.h"
  7. @implementation SearchBar
  8. - (id)initWithFrame:(CGRect)frame
  9. {
  10. self = [super initWithFrame:frame];
  11. if (self) {
  12. self.font = [UIFont systemFontOfSize:15];
  13. self.placeholder = @" 输入品种关键字";
  14. //设置边框和边框颜色
  15. self.borderStyle=UITextBorderStyleNone;
  16. self.layer.cornerRadius=15.0f;
  17. self.layer.masksToBounds=YES;
  18. self.layer.borderColor=[[UIColor colorWithRed:224/255.0 green:243/255.0 blue:223/255.0 alpha:1.0]CGColor];
  19. self.layer.borderWidth= 2.0f;
  20. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
  21. [button setBackgroundImage:[UIImage imageNamed:@"seachBar_rightView"] forState:UIControlStateNormal];
  22. [button setTitle:@"搜索" forState:UIControlStateNormal];
  23. [button setTitleColor:[UIColor colorWithRed:119/255.0 green:166/255.0 blue:16/255.0 alpha:1.0] forState:UIControlStateNormal];
  24. button.titleLabel.font = [UIFont systemFontOfSize:18.0];
  25. button.titleLabel.textColor = [UIColor colorWithRed:119/255.0 green:166/255.0 blue:16/255.0 alpha:1.0];
  26. button.titleLabel.textColor = [UIColor redColor];
  27. self.rightView = button;
  28. self.rightViewMode = UITextFieldViewModeAlways;
  29. //这里设置光标位置,让光标位置后移10
  30. self.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
  31. self.leftViewMode = UITextFieldViewModeAlways;
  32. }
  33. return self;
  34. }
  35. + (instancetype)searchBar
  36. {
  37. return [[self alloc] init];
  38. }
  39. @end
+
 

iOS---searchBar 搜索框 光标初始位置后移