vendredi 30 janvier 2015

How to add custom border to multiple UITextField?

I am working on a project which have number of text fields. All text fields should have custom border at bottom and sides. enter image description here I found this answer



CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor blueColor].CGColor;
[self.textField.layer addSublayer:bottomBorder];

CALayer *leftBorder = [CALayer layer];
leftBorder.frame = CGRectMake(0.0f, self.frame.size.height -5, 1.0f, 5.0f);
leftBorder.backgroundColor = [UIColor lightGrayColor].CGColor;
[self.textField.layer addSublayer:bottomBorder];

CALayer *rightBorder = [CALayer layer];
rightBorder.frame = CGRectMake(self.frame.size.width -1, self.frame.size.height -5, 1.0f, 5.0f);
rightBorder.backgroundColor = [UIColor lightGrayColor].CGColor;
[self.textField.layer addSublayer:rightBorder];


But by using this method I need to add layer for each textField separately.


EDIT: I have achieved this by subclassing UITextField as suggested in comments.


But the borders of text fields are getting distorted as the view is scrolling. I am using auto-layout in storyboard. I have a UIScrollView(scrollView) in ViewController main view(self.view) and a UIView(contentView) within scrollView.All the TextView's are placed in contentView.


View hierarchy -



MainView
ScrollView
ContentView
TextField's



Aucun commentaire:

Enregistrer un commentaire