# 自定义UITextField实现只带下划线的输入框

**URL:** <https://www.shiqidu.com/t/topic/551>\
**Category:** 开发调优\
**Tags:** C-C-Obj-C\
**Created:** [2019 年2 月 16 日 04:25 UTC](https://www.shiqidu.com/t/topic/551 "2019-02-16T04:25:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2019 年2 月 16 日 04:25 UTC](https://www.shiqidu.com/t/topic/551/1 "2019-02-16T04:25:47Z")

</div>

很多APP登录注册的输入框都是四周没有边框，只有下边框有一条线，比如微信等。但是xib中却没有这种样式，想要实现这种效果，我们可以自定义一个UITextField，实现drawRect方法。

```oc
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, SQDColor(231, 236, 239).CGColor);
    CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - 1, CGRectGetWidth(self.frame), 1));
}

```

效果图

![alt text](https://discourse.shiqidu.com/upload/discussion/20190216/2061566063ece2b005b1e74a01d2e3a9_origin.png)
