須要爲UITextField新建一個分類,新建一個函數,利用UIBezierPath中的bezierPathWithRoundedRect方法創建一個圓角矩形遮罩便可,顯示效果很是好。app
其中corners能夠指定哪一個角爲圓角。函數
注意此方法只可設置輸入區域爲圓角,不能設置borderspa
- (void)applyRoundCorners:(UIRectCorner)corners radius:(CGFloat)radiusit
{class
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];方法
CAShapeLayer *maskLayer = [CAShapeLayer layer];apply
maskLayer.frame = self.bounds;di
maskLayer.path = maskPath.CGPath;co
self.layer.mask = maskLayer;path
}
設置border可用下面代碼
_searchInputTextField.layer.cornerRadius=15.0f;
_searchInputTextField.layer.masksToBounds=YES;