Why bottom border line of textField is showing half? - ios

I have 5 textFields in my UIViewController and I have to show bottom border line on these all textFields.
I am using FloatLabelTextField class and trying to set bottom border on each textField by programatically but very first time (in viewDidLoad method) border line is just showing half. I don't know why this is showing.
Here is my code:
import UIKit
class ViewController: UITableViewController {
#IBOutlet var vwName: FloatLabelTextField!
override func viewDidLoad() {
super.viewDidLoad()
vwName.delegate = self
setBottomBorder(textField: vwName, width: 0.8, color: .darkGray)
}
}
extension ViewController : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if let sublayers = textField.layer.sublayers {
for layer: CALayer in sublayers {
if layer.name == "BottomBorder" {
layer.removeFromSuperlayer()
}
}
}
setBottomBorder(textField: textField, width: 0.8, color: .blue)
}
func textFieldDidEndEditing(_ textField: UITextField) {
if let sublayers = textField.layer.sublayers {
for layer: CALayer in sublayers {
if layer.name == "BottomBorder" {
layer.removeFromSuperlayer()
}
}
}
setBottomBorder(textField: textField, width: 0.8, color: .lightGray)
textField.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
extension ViewController {
func setBottomBorder(textField: UITextField, width: CGFloat, color: UIColor) {
let border = CALayer()
border.name = "BottomBorder"
border.borderColor = color.cgColor
border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width: textField.frame.size.width, height: width)
border.borderWidth = width
textField.borderStyle = UITextBorderStyle.none
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
}
}
If I tap on a text field then border line is coming properly. I mean when TextFieldDidBeginEditing delegate method call then border line showing properly.
But initially when view load then it is showing half.

Related

(Swift 3) - How to include animations in custom UIView

I have a UIView subclass called View, in which I want a "ripple" to move outwards from wherever the user double clicks (as well as preforming other drawing functions). Here is the relevant part of my draw method, at present:
override func draw(_ rect: CGRect) {
for r in ripples {
r.paint()
}
}
This is how r.paint() is implemented, in class Ripple:
func paint() {
let c = UIColor(white: CGFloat(1.0 - (Float(iter))/100), alpha: CGFloat(1))
print(iter, " - ",Float(iter)/100)
c.setFill()
view.fillCircle(center: CGPoint(x:x,y:y), radius: CGFloat(iter))
}
iter is supposed to be incremented every 0.1 seconds by a Timer which is started in the constructor of Ripple:
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: move)
move is implemented as follows:
func move(_ timer: Timer) {
while (tier<100) {
iter += 1
DispatchQueue.main.async {
self.view.setNeedsDisplay()
}
}
timer.invalidate()
}
What is supposed to happen is that every 0.1 seconds when the timer fires, it will increment iter and tell the View to repaint, which it will then do using the Ripple.paint() method, which uses iter to determine the color and radius of the circle.
However, as revealed using print statements, what happens instead is that the Timer fires all 100 times before the redraw actually takes place. I have tried dealing with this by replacing DispatchQueue.main.async with DispatchQueue.main.sync, but this just made the app hang. What am I doing wrong? If this is just not the right way to approach the problem, how can I get a smooth ripple animation (which involves a circle which grows while changing colors) to take place while the app can still preform other functions such as spawning more ripples? (This means that multiple ripples need to be able to work at once.)
You can achieve what you want with CABasicAnimation and custom CALayer, like that:
class MyLayer : CALayer
{
var iter = 0
override class func needsDisplay(forKey key: String) -> Bool
{
let result = super.needsDisplay(forKey: key)
return result || key == "iter"
}
override func draw(in ctx: CGContext) {
UIGraphicsPushContext(ctx)
UIColor.red.setFill()
ctx.fill(self.bounds)
UIGraphicsPopContext()
NSLog("Drawing, \(iter)")
}
}
class MyView : UIView
{
override class var layerClass: Swift.AnyClass {
get {
return MyLayer.self
}
}
}
class ViewController: UIViewController {
let myview = MyView()
override func viewDidLoad() {
super.viewDidLoad()
myview.frame = self.view.bounds
self.view.addSubview(myview)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let animation = CABasicAnimation(keyPath: "iter")
animation.fromValue = 0
animation.toValue = 100
animation.duration = 10.0
myview.layer.add(animation, forKey: "MyIterAnimation")
(myview.layer as! MyLayer).iter = 100
}
}
Custom UIView like AlertView with Animation for Swift 3 and Swift 4
You can use a extension:
extension UIVIew{
func customAlertView(frame: CGRect, message: String, color: UIColor, startY: CGFloat, endY: CGFloat) -> UIVIew{
//Adding label to view
let label = UILabel()
label.frame = CGRect(x: 0, y: 0, width: frame.width, height:70)
label.textAlignment = .center
label.textColor = .white
label.numberOfLines = 0
label.text = message
self.addSubview(label)
self.backgroundColor = color
//Adding Animation to view
UIView.animate(withDuration:0.5, delay: 0, options:
[.curveEaseOut], animations:{
self.frame = CGRect(x: 0, y: startY, width: frame.width, height: 64)
}) { _ in
UIView.animate(withDuration: 0.5, delay: 4, options: [.curveEaseOut], animations: {
self.frame = CGRect(x: 0, y: endY, width: frame.width, height:64)
}, completion: {_ in
self.removeFromSuperview()
})
}
return self
}
And you can use it un your class. This example startY is when you have a navigationController:
class MyClassViewController: UIViewController{
override func viewDidLoad(){
super.viewDidLoad()
self.view.addSubView(UIView().addCustomAlert(frame: self.view.frame, message: "No internet connection", color: .red, startY:64, endY:-64))
}
}

CALayer border colour and width are not changing?

I have 5 textFields and my requirement is I have to set 0.2 border width initially, after that when tap on edit button I have to change border width from 0.2 to 0.8 and again tap on submit button I have to change border width from 0.8 to 0.2.
The thing is that border colour and width are not changing. Here below is my code:
class EditProductCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
//Create Border Line.
createBorderLine(0.2,UIColor.lightGray)
}
//Create Border Line on Text Field
func createBorderLine(_ width : CGFloat, _ color : UIColor)//_ width : CGFloat, _ color : UIColor)
{
setBottomBorder(textField: InvoiceDate, width: width,color : color)
setBottomBorder(textField: InvoiceNumber, width: width,color : color)
setBottomBorder(textField: modelNumber, width: width,color : color)
setBottomBorder(textField: productName, width: width,color : color)
setBottomBorder(textField: serialNumber, width: width,color : color)
self.layoutSubviews()
}
}
Here is another class:
class EditProductView: BaseViewController {
//TableView
#IBOutlet var tableView: UITableView!
//View Did Load
override func viewDidLoad() {
super.viewDidLoad()
//self.view.backgroundColor = hexStringToUIColor(hex: "#CCCCCC")
tableView.delegate = self
tableView.dataSource = self
//Button Submit
self.btnSubmit.isHidden = true
tableView.bounces = false
tableView.alwaysBounceVertical = false
hideKeyboardWhenTappedAround()
}
//Button Submit
#IBAction func btnSubmitAction(_ sender: Any) {
self.btnSubmit.isHidden = true
let index : NSIndexPath = NSIndexPath(row: 0, section: 0)
let tCell : EditProductCell = self.tableView.cellForRow(at: index as IndexPath) as! EditProductCell
//Change border line
tCell.createBorderLine(0.2, UIColor.lightGray)
tCell.btnEdit.isHidden = false
dismissKeyboard()
}
func btnEditAction()
{
btnSubmit.isHidden = false
let index : NSIndexPath = NSIndexPath(row: 0, section: 0)
let tCell : EditProductCell = self.tableView.cellForRow(at: index as IndexPath) as! EditProductCell
tCell.btnEdit.isHidden = true
//Create border line
tCell.createBorderLine(0.8, UIColor.black)
dismissKeyboard()
}
}
Here is set bottom border method in separate class:
//Set Bottom border line.
func setBottomBorder(textField: UITextField, width: CGFloat,color : UIColor) {
let border = CALayer()
border.name = "BottomBorder"
border.borderColor = color.cgColor
border.frame = CGRect(x: 0, y: textField.frame.size.height - width,
width: textField.frame.size.width, height: width)
border.borderWidth = width
textField.borderStyle = UITextBorderStyle.none
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
}
You can see in my code that button submit and button edit. I am changing border line colour and width. Colour and width are not.
I also tried something in but not able to get width change.
override func layoutSublayers(of layer: CALayer) {
if (layer == self.layer)
{
layer.borderWidth = 0.3
}
}
//MARK:- TextField Delegate
extension EditProductCell : UITextFieldDelegate
{
func textFieldDidBeginEditing(_ textField: UITextField) {
if let sublayers = textField.layer.sublayers {
for layer: CALayer in sublayers {
if layer.name == "BottomBorder" {
layer.removeFromSuperlayer()
}
}
}
setBottomBorder(textField: textField, width: 0.8, color: hexStringToUIColor(hex: "#55ACEE"))
}
//TextField Did End Editing
func textFieldDidEndEditing(_ textField: UITextField) {
if let sublayers = textField.layer.sublayers {
for layer: CALayer in sublayers {
if layer.name == "BottomBorder" {
layer.removeFromSuperlayer()
}
}
}
setBottomBorder(textField: textField, width: 0.8,color : UIColor.black)
textField.resignFirstResponder()
}
//TextField Return Key
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Try to find next responder
if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField
{
nextField.becomeFirstResponder()
}
else
{
textField.resignFirstResponder()
}
return false
}
}
Swift 4
shape.strokeColor = UIColor.lightGray.cgColor
shape.lineWidth = 1.0
If you try with numbers greater than 1 your code works. I think the border width need to be greater than 1.

How do i tell my UIView that the button in UIViewController has been pressed? (Swift)

Is it possible to pass data between the UIViewController and the UIView?
if yes, how?
I can not find anything on the internet which explains it.
For example when you press button1 the UIViewController says to the UIView that the person has pressed button1 and then the UIView draws a Circle. But if you then press button2 the UIViewController says to the UIView that he has now pressed button2 and then the UIView eliminates the Circle and draws a triangle.
The buttons are built programmatically.
The question is how I can invoke the shapes which I have drawn with drawRect within the UIView in the UIViewController.
class ViewController: UIViewController {
#IBOutlet var UnitViewTabeller: UnitView!
var btn: UIButton!
override viewDidLoad {
self.btn = UIButton(frame: CGRectMake(0.04 * view.bounds.width, 0.91 * view.bounds.height, 0.44 * view.bounds.width, 0.07 * view.bounds.height)) //set frame
self.btn.layer.cornerRadius = 10
self.btn.setTitle("0", forState: .Normal) //set button title
self.btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
self.btn.backgroundColor = UIColor(red: 0.2745, green: 0.2784, blue: 0.2706, alpha: 1.0) //set button background color
self.btn.tag = 0 // set button tag
self.btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btn) //add button in view
self.btnArray.append(btw)
}
func btnclicked(sender: UIButton) {
//draw the Circle from the UIView
}
}
class CircleView: UIView {
override func drawRect(rect: CGRect) {
let Circle = UIGraphicsGetCurrentContext()
let rectangle = CGRect(x: 0.2 * bounds.width, y: 0.15 * bounds.height, width: 0.6 * bounds.width, height: 0.6 * bounds.width)
CGContextSetLineWidth(Circle, 4.0)
CGContextAddEllipseInRect(Circle, rectangle)
CGContextStrokePath(Circle)
}
}
Here is an example in Swift 3 of a ShapeView that redraws itself when its shape property is set:
class ShapeView: UIView {
var shape: Shape = .blank {
didSet {
// calling setNeedsDisplay() triggers a redraw of ShapeView
setNeedsDisplay()
}
}
enum Shape {
case circle
case triangle
case blank
}
override func draw(_ rect: CGRect) {
switch shape {
case .circle: drawCircle()
case .triangle: drawTriangle()
default: break
}
}
func drawCircle() {
let circle = UIGraphicsGetCurrentContext()
let rectangle = CGRect(x: 0.2 * bounds.width, y: 0.15 * bounds.height, width: 0.6 * bounds.width, height: 0.6 * bounds.width)
circle?.setLineWidth(4.0)
circle!.addEllipse(in: rectangle)
circle!.strokePath()
}
func drawTriangle() {
// code to draw a triangle
}
}
Assuming you have a ShapeView object called shapeView as a property of your ViewController, you'd call it like this:
func drawCircleButton(_ sender: UIButton) {
shapeView.shape = .circle
}
func drawTriangleButton(_ sender: UIButton) {
shapeView.shape = .triangle
}

Setting the frame of a sublayer inside of layoutSubviews() makes changing it impossible. Any workaround for that?

I'm working on a customizable UITextField (see code below). I have added a border at the bottom (you can set it in the storyboard). However, I had problems setting the frame of the CALayer that this border consists of.
If I set it inside the didSet method of var showBottomBorder it doesn't appear on the screen. I think this is because the frame (of the UITextField) hasn't been calculated yet (maybe didSet gets called before that).
So I moved it to the layoutSubviews() method (see code below). This works perfectly.
But now I have another problem. I can't really change that frame anymore. Every time I change it, it gets reset by layoutSubviews() which I think is called then.
At the bottom of my code, there is the method textFieldDidBeginEditing. In there, I wanted to move up my bottom border (animated). But it doesn't work. The border does not move anywhere. And like I said, I think it's because I set the frame inside the layoutSubviews() method.
Is there a better way to set the frame of the bottom border? A way which allows me to change stuff?
#IBDesignable
class CustomizableTextField: UITextField, UITextFieldDelegate {
// MARK: - Properties
private var bottomBorder = CALayer()
// MARK: - #IBInspectables
#IBInspectable var roundCorners: CGFloat = 0 {
didSet {
self.layer.cornerRadius = roundCorners
self.clipsToBounds = true
}
}
/** -- */
#IBInspectable var borderWidth: CGFloat = 1.0 {
didSet {
self.layer.borderWidth = self.borderWidth
}
}
#IBInspectable var borderColor: UIColor = UIColor.white {
didSet {
self.layer.borderColor = self.borderColor.cgColor
}
}
/** -- */
/** -- */
private var showBottomBorder: Bool = false {
didSet {
switch showBottomBorder {
case true:
bottomBorder.borderColor = self.bottomBorderColor.cgColor
bottomBorder.borderWidth = self.bottomBorderWidth
self.layer.addSublayer(bottomBorder)
self.layer.masksToBounds = true
break
case false:
bottomBorder.removeFromSuperlayer()
break
}
}
}
#IBInspectable var bottomBorderWidth: CGFloat = 1.0 {
didSet {
self.showBottomBorder = false
self.showBottomBorder = true
}
}
#IBInspectable var bottomBorderColor: UIColor = UIColor.white {
didSet {
self.showBottomBorder = false
self.showBottomBorder = true
}
}
/** -- */
/** -- */
// Somwhow, the default panel for my font color doesn't change anything, so I created this
#IBInspectable var fixedFontColor: UIColor = UIColor.white {
didSet {
self.textColor = fixedFontColor
}
}
#IBInspectable var placeholderFontColor: UIColor = UIColor.white {
didSet {
var placeholderTxt = ""
if let txt = self.placeholder {
placeholderTxt = txt
}
self.attributedPlaceholder = NSAttributedString(string: placeholderTxt, attributes: [NSForegroundColorAttributeName: placeholderFontColor])
}
}
/** -- */
// MARK: - Overrides and Initializers
override init(frame: CGRect) {
super.init(frame: frame)
}
override func layoutSubviews() {
super.layoutSubviews()
// HERE
bottomBorder.frame = CGRect(x: 0, y: self.frame.size.height - self.bottomBorderWidth, width: self.frame.size.width, height: self.frame.size.height)
}
// setting the textField delegate to self
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//self.borderStyle = .none
self.delegate = self
}
// MARK: - Events
func textFieldDidBeginEditing(_ textField: UITextField) {
}
You can use and extension on UITextFiled for setting the border.
And keep a reference to it with KVC.
By overriding LayoutSubview, every the layout will change, we'l check if the border exists, if so remove it, and re-create a new one with the new frame:
import UIKit
let MyTopBorder = "myTopBorder"
let MyBottomBorder = "myBottomBorder"
struct Defaults {
static let width = CGFloat(1.0)
static func bottonBorderFrame(view: UIView)->CGRect {
return CGRect(x: CGFloat(0), y: view.frame.size.height - Defaults.width, width: view.frame.size.width, height: view.frame.size.height)
}
static func topBorderFrame(view: UIView)->CGRect {
return CGRect(x: CGFloat(0), y: CGFloat(0) , width: view.frame.size.width, height: Defaults.width)
}
}
extension UITextField
{
func setBottomBorder(color:CGColor)
{
if let isBottomBorder = self.getBottomBorderIfExists() {
isBottomBorder.removeFromSuperlayer()
}
self.setBorderWithFrame(Defaults.bottonBorderFrame(self), color: color, andKey: MyBottomBorder)
}
func setTopBorder(color:CGColor)
{
if let isTopBorder = self.getTopBorderIfExists() {
isTopBorder.removeFromSuperlayer()
}
self.setBorderWithFrame(Defaults.topBorderFrame(self), color: color, andKey: MyTopBorder)
}
func setBorderWithFrame(frame: CGRect, color: CGColor, andKey: String) {
self.borderStyle = UITextBorderStyle.None;
let border = CALayer()
border.borderColor = color
border.frame = frame
border.borderWidth = Defaults.width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
self.layer.setValue(border, forKey: andKey)
}
func removeTopBorder() {
if let isTopBorder = self.getTopBorderIfExists() {
self.layer.setValue(nil, forKey: MyTopBorder)
isTopBorder.removeFromSuperlayer()
}
}
func removeBottomBorder() {
if let isBottomBorder = self.getBottomBorderIfExists() {
self.layer.setValue(nil, forKey: MyBottomBorder)
isBottomBorder.removeFromSuperlayer()
}
}
private func getBorderIfExistsByKey(key: String)->CALayer? {
if let isBorderSet = self.layer.valueForKey(key) {
if let borderIsCALayer = isBorderSet as? CALayer {
return borderIsCALayer
}
}
return nil
}
private func getTopBorderIfExists()->CALayer? {
return self.getBorderIfExistsByKey(MyTopBorder)
}
private func getBottomBorderIfExists()->CALayer? {
return self.getBorderIfExistsByKey(MyBottomBorder)
}
public override func layoutSubviews() {
super.layoutSubviews()
// Update bottom on frame change
if let isBottomBorder = self.getBottomBorderIfExists() {
let borderColor = isBottomBorder .borderColor
self.removeBottomBorder()
self.setBottomBorder(borderColor!)
}
// Update top on frame change
if let isTopBorder = self.getTopBorderIfExists() {
let borderColor = isTopBorder.borderColor
self.removeTopBorder()
self.setTopBorder(borderColor!)
}
}
}
Usage:
let textField = UITextField(frame: CGRect(x: 100,y: 100, width: 100, height: 100))
textField.backgroundColor = UIColor.blueColor() // Thie color is for visulizing better
self.view.addSubview(textField)
textField.setBottomBorder(UIColor.blackColor().CGColor) // Now you have a border
textField.frame = CGRect(x: 150, y: 200, width: 200, height: 200) // And the border updated to the new frame
// Now if you would like to change from bottom to top, simply do this:
textField.removeBottomBorder()
textField.setTopBorder(UIColor.blackColor().CGColor)

Creating and removing border line on textFields

Actually i am trying to change border colour of each text field(By increasing border width) on tap.
I have given textFieldEmailAddress.tag = 1 and textFieldPassword.tag = 2 by using stotyBoard.
I am using UITextFieldDelegate.
Means my requirement is :-
1. when i click on a text field its border colour should be dark blue(width =0.8)
and other text fields border line colour should be light blue (width = 0.4)
If we don't click on a text field its border line colour should be light blue.(width = 0.4).
But i am facing a problem when click on text field first time its working fine ,but just when i tap on another text field ,its giving dark blue border colour.
problem is whaever textfield became 0.8 it's not reverting to 0.4 in UI (means UI is not updating only value is getting updated to 0.4)
Is any thing wrong in delegate method or in my below code :-
//View Did load function
override func viewDidLoad()
{
//Set delegate
self.textFieldEmailAddress.delegate = self
self.textFieldPassword.delegate = self
//Create border line on text email address fields.
self.borderline(textFieldEmailAddress)
//Create border line on text password fields.
self.borderline(textFieldPassword)
}
func textFieldDidBeginEditing(textField: UITextField)
{
switch textField.tag
{
case 1:
self.Darkborderline(textFieldEmailAddress)
break
case 2:
self.Darkborderline(textFieldPassword)
break
default:
break
}
}
func textFieldDidEndEditing(textField: UITextField)
{
switch textField.tag
{
case 1:
self.borderline(textFieldEmailAddress)
break
case 2:
self.borderline(textFieldPassword)
break
default:
break
}
}
//Light blue Border line on text field.
func borderline(textField : UITextField)
{
switch textField.tag
{
case 1:
width = CGFloat(0.40)
let border = CALayer()
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: textFieldEmailAddress.frame.size.height - width,
width: textFieldEmailAddress.frame.size.width, height: width)
border.borderWidth = width
textFieldEmailAddress.borderStyle = UITextBorderStyle.None
textFieldEmailAddress.layer.addSublayer(border)
textFieldEmailAddress.layer.masksToBounds = true
break
case 2:
width = CGFloat(0.40)
let border = CALayer()
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: textFieldPassword.frame.size.height - width,
width: textFieldPassword.frame.size.width, height: width)
border.borderWidth = width
textFieldPassword.borderStyle = UITextBorderStyle.None
textFieldPassword.layer.addSublayer(border)
textFieldPassword.layer.masksToBounds = true
break
default: break
}
}
//Dark blue Border line on text field.
func Darkborderline(textField : UITextField)
{
//let border = CALayer()
switch textField.tag
{
case 1:
width = CGFloat(0.80)
let border = CALayer()
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: textFieldEmailAddress.frame.size.height - width,
width: textFieldEmailAddress.frame.size.width, height: width)
border.borderWidth = width
textFieldEmailAddress.borderStyle = UITextBorderStyle.None
textFieldEmailAddress.layer.addSublayer(border)
textFieldEmailAddress.layer.masksToBounds = true
break
case 2:
width = CGFloat(0.80)
let border = CALayer()
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: textFieldPassword.frame.size.height - width,
width: textFieldPassword.frame.size.width, height: width)
border.borderWidth = width
textFieldPassword.borderStyle = UITextBorderStyle.None
textFieldPassword.layer.addSublayer(border)
textFieldPassword.layer.masksToBounds = true
break
default: break
}
}
First declare one method to set bottom border to field and use the delegate method of UITextFieldDelegate like this
On viewDidLoad first assign thin border like this to both the fields
override func viewDidLoad() {
super.viewDidLoad()
self.setBottomBorder(self.textFieldEmailAddress, width: 0.4)
self.setBottomBorder(self.textFieldPassword, width: 0.4)
self.setBottomBorder(self.passwordBtn, width: 0.4)
}
func setBottomBorder(view: UIView, width: CGFloat) {
let border = CALayer()
border.name = "BottomBorder"
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: view.frame.size.height - width,
width: view.frame.size.width, height: width)
border.borderWidth = width
view.borderStyle = UITextBorderStyle.None
view.layer.addSublayer(border)
view.layer.masksToBounds = true
}
func removeBottomBorder(view: UIView) {
if let sublayers = view.layer.sublayers {
for layer: CALayer in sublayers {
if layer.name == "BottomBorder" {
layer.removeFromSuperlayer()
break
}
}
}
}
func textFieldDidBeginEditing(textField: UITextField) {
self.removeBottomBorder(textField)
self.setBottomBorder(textField, width: 0.8)
if (textFieldPassword == textField) {
self.setBottomBorder(self.passwordBtn, width: 0.8)
}
}
func textFieldDidEndEditing(textField: UITextField) {
self.removeBottomBorder(textField)
self.setBottomBorder(textField, width: 0.4)
if (textFieldPassword == textField) {
self.removeBottomBorder(self.passwordBtn)
self.setBottomBorder(self.passwordBtn, width: 0.4)
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Try this once .
func textFieldShouldBeginEditing(textField: UITextField) -> Bool
{
if 1 == textField.tag
{
selectedTextField = textFieldEmailAddress
self.selectedTextField.layer.borderColor = UIColor.blueColor().CGColor
self.selectedTextField.layer.borderWidth = 0.8
self.selectedTextField = textField
self.textFieldPassword.layer.borderColor = UIColor.blueColor().CGColor
self.textFieldPassword.layer.borderWidth = 0.4
return true
}
else if 2 == textField.tag
{
selectedTextField = textFieldPassword
self.selectedTextField.layer.borderColor = UIColor.blueColor().CGColor
self.selectedTextField.layer.borderWidth = 0.8
self.selectedTextField = textField
self.textFieldEmailAddress.layer.borderColor = UIColor.blueColor().CGColor
self.textFieldEmailAddress.layer.borderWidth = 0.4
return true
}
else
{
return false
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
self.selectedTextField.layer.borderColor = UIColor.blueColor().CGColor
self.selectedTextField.layer.borderWidth = 0.4
self.selectedTextField = UITextField()
textField.resignFirstResponder()
return true
}

Resources