使用textField来画搜索框

-(void)createTitleView{
    HMSQFoodSquareSearchTF * search = [[HMSQFoodSquareSearchTF alloc]initWithFrame:CGRectMake(0, 0, 277*PW,30)];
//    search.borderStyle = UITextBorderStyleLine;
    search.layer.cornerRadius = 15;
    UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 11*PW, 11*PW)];
    imageV.image = [UIImage imageNamed:@"hdj_sousuoxiao"];
    search.leftView = imageV;
    search.leftViewMode = UITextFieldViewModeAlways;

    search.backgroundColor = [UIColor whiteColor];
    search.layer.masksToBounds = YES;
    search.placeholder = @"请输入商家品或分类";
    search.font = [UIFont systemFontOfSize:13*PW];
    [search setTextAlignment:NSTextAlignmentLeft];
    UIView * baseView = [[UIView alloc]initWithFrame:search.bounds];
    [baseView addSubview:search];
    baseView.layer.shadowColor = [UIColor colorWithHexString:@"#CDCDCD" alpha:1].CGColor;
    baseView.layer.shadowOpacity = 0.4;
    baseView.layer.masksToBounds = NO;
    baseView.layer.shadowOffset = CGSizeMake(1, 1);
    self.navigationItem.titleView = baseView;
    
    
    
}
#import "HMSQFoodSquareSearchTF.h"

@implementation HMSQFoodSquareSearchTF

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)drawPlaceholderInRect:(CGRect)rect{
    CGSize placeholderSize = [self.placeholder sizeWithAttributes:
                              @{NSFontAttributeName : self.font}];

    [self.placeholder drawInRect:CGRectMake(3*PW, (rect.size.height - placeholderSize.height)/2.0, rect.size.width, rect.size.height) withAttributes:
     @{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#999999" alpha:1],
       NSFontAttributeName : self.font}];

}
-(CGRect)leftViewRectForBounds:(CGRect)bounds{
    return CGRectMake(71*PW, (30-11*PW)/2.0, 11*PW, 11*PW);
}
@end

你可能感兴趣的:(使用textField来画搜索框)