Subclass a UITextField to create a border only on the bottom. Note: Both the header (.h) and the implementation (.m) files are combined here, just to make this example a single file. Adapted from: http://stackoverflow.com/a/29428428/155167.
// Makes sense to break this out into its own header file.
#import <UIKit/UIKit.h>
@interface BottomBorderTextField : UITextField
@end
// Here would begin the implementation file.
//#import "BottomBorderTextField.h"
@interface BottomBorderTextField ()
@property (weak, nonatomic) CALayer* bottomBorder;
@end
@implementation BottomBorderTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_bottomBorder = [self initializeBottomBorder];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
_bottomBorder = [self initializeBottomBorder];
}
return self;
}
- (CALayer *)initializeBottomBorder
{
CALayer* border = [CALayer layer];
CGFloat borderWidth = 0.5;
border.borderColor = [UIColor lightGrayColor].CGColor;
border.borderWidth = borderWidth;
[[self layer] addSublayer:border];
[[self layer] setMasksToBounds:YES];
return border;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.bottomBorder.frame = CGRectMake(0,
self.frame.size.height - self.bottomBorder.borderWidth,
self.frame.size.width,
self.frame.size.height);
}
@end