1.在导航栏添加一个UITextField代码:
self.searchTF = [[UITextField alloc]initWithFrame:CGRectMake(50, 0, SCREEN_SIZE_WIDTH-100, 35)];
self.searchTF.font = MIDDLE_FONT;
self.searchTF.layer.cornerRadius = BUTTON_RADUIS;
self.searchTF.placeholder = @"化妆品";
self.searchTF.tintColor = [UIColor whiteColor];
self.searchTF.textColor = [UIColor whiteColor];
self.searchTF.delegate = self;
self.searchTF.backgroundColor = MAIN_THEME_COLOR;
self.navigationItem.titleView = self.searchTF;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
// 设置富文本对象的颜色
attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
// 设置UITextField的占位文字
self.searchTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号" attributes:attributes];
//左侧设置放大镜
self.searchTF.leftViewMode = UITextFieldViewModeAlways;
UIImageView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(5, (self.searchTF.bounds.size.height-20)/2, 20, 20)];
leftView.image = [UIImage imageNamed:@"temp_fangdajing"];
self.searchTF.leftView = leftView;
2.改变光标颜色
self.searchTF.tintColor = [UIColor whiteColor];
3.改变占位符颜色
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
// 设置富文本对象的颜色
attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
// 设置UITextField的占位文字
self.searchTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号" attributes:attributes];
4.设置左侧的站位图放大镜
//左侧设置放大镜
self.searchTF.leftViewMode = UITextFieldViewModeAlways;
UIImageView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(5, (self.searchTF.bounds.size.height-20)/2, 20, 20)];
leftView.image = [UIImage imageNamed:@"temp_fangdajing"];
self.searchTF.leftView = leftView;
5.效果