TextField Border not showing in iphone below 6 Devices - ios

I have create bottom border of textfields it is showing on all devices from 6 and above but on below 6 it is not showing ..it is showing empty placeholder I have tested on 5s
I am using this code
func designTextfields(){
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: 0, y: emailField.frame.size.height - width, width: emailField.frame.size.width, height: emailField.frame.size.height)
border.borderWidth = width
emailField.layer.addSublayer(border)
emailField.layer.masksToBounds = true
let border1 = CALayer()
let width1 = CGFloat(2.0)
border1.borderColor = UIColor.darkGray.cgColor
border1.frame = CGRect(x: 0, y: passField.frame.size.height - width1, width: passField.frame.size.width, height: passField.frame.size.height)
border1.borderWidth = width1
passField.layer.addSublayer(border1)
passField.layer.masksToBounds = true
}

Related

Creating two textfields with only bottom border swift

I'm trying to create two UITextFields that both only have the bottom border, I got some code from another StackOverflow and it worked. However, when I copy and pasted the code again for my second TextField it only shows one TextField with the bottom border.
Here is the code:
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.gray.cgColor
border.frame = CGRect(x: 0, y: emailText.frame.size.height -
width, width: emailText.frame.size.width, height:
emailText.frame.size.height)
border.frame = CGRect(x: 0, y: passwordText.frame.size.height
- width, width: passwordText.frame.size.width, height:
passwordText.frame.size.height)
border.borderWidth = width
passwordText.layer.addSublayer(border)
passwordText.layer.masksToBounds = true
border.borderWidth = width
emailText.layer.addSublayer(border)
emailText.layer.masksToBounds = true
Thanks for the help!
CALayers are instance-based, that means if you added the layer to a view, you cannot add it to a new one without removing it from the first view. Create two instances of CALayers or better yet, make an extension that produces the desired result.
Create separate border object for email textfield and username textfield.
let emailborder = CALayer()
let passwordborder = CALayer()
let width = CGFloat(2.0)
emailborder.borderColor = UIColor.gray.cgColor
emailborder.frame = CGRect(x: 0, y: emailText.frame.size.height -
width, width: emailText.frame.size.width, height:
emailText.frame.size.height)
passwordborder.frame = CGRect(x: 0, y: passwordText.frame.size.height
- width, width: passwordText.frame.size.width, height:
passwordText.frame.size.height)
passwordborder.borderWidth = width
passwordText.layer.addSublayer(passwordborder)
passwordText.layer.masksToBounds = true
emailborder.borderWidth = width
emailText.layer.addSublayer(emailborder)
emailText.layer.masksToBounds = true
You cant use the same view for both textfield. Use a copy of border for the second one. Easiest way to make this work is to refine a variable as border2 and set it up and sublayer it. Don't forget to remove your duplicate frame assign for boarder.
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.gray.cgColor
border.frame = CGRect(x: 0, y: emailText.frame.size.height - width, width: emailText.frame.size.width, height: emailText.frame.size.height)
let border2 = CALayer() border2.borderColor = border.borderColor border2.frame = border.frame;
border.borderWidth = width
passwordText.layer.addSublayer(border)
passwordText.layer.masksToBounds = true
border.borderWidth = width
emailText.layer.addSublayer(border2)
emailText.layer.masksToBounds = true
The best you could without writing redundant code is that you create a function, pass the textfields you want to border as parameters and keep the styling code in there like so:
func setBorder(tf: UITextField) {
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.gray.cgColor
border.frame = CGRect(x: 0, y: tf.frame.size.height - width,
width: tf.frame.size.width, height:
tf.frame.size.height)
border.borderWidth = width
tf.layer.addSublayer(border)
tf.layer.masksToBounds = true
}
Call this function in viewDidLoad:
setBorder(tf: emailText)
setBorder(tf: passwordText)
Hope this helps!

Swift make input transparent with only border-bottom

How can I create a textField with transparent background and only border bottom?
I have tried this code:
textField.backgroundColor = .clear
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.darkGrayColor().CGColor
border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width: textField.frame.size.width, height: textField.frame.size.height)
border.borderWidth = width
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
But it isn't working.
Here's an alternative implementation using a UIView rather than a CALayer.
let line = UIView()
line.frame.size = CGSize(width: textField.frame.size.width, height: 1)
line.frame.origin = CGPoint(x: 0, y: textField.frame.maxY - line.frame.height)
line.backgroundColor = UIColor.darkGray
line.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
textField.addSubview(line)
Your code is correct. Problem is only with your border height which is now "textField.frame.size.height" change it to 1.0.(Change your border frame code).
Please try this :
txtField .borderStyle = .none
It may help you.... :)
extension UITextField {
func setBottomBorder(borderColor: CGColor = UIColor.white.cgColor,
backgroundColor: CGColor = UIColor.clear.cgColor) {
self.borderStyle = .none
self.layer.backgroundColor = backgroundColor
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = borderColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
This worked for me.
Then you can just call
yourTextField.SetBottomBorder(borderColor: UIColor.white.cgColor, backgroundColor: UIColor.clear.cgColor)
Just add this line with your code :-
border.backgroundColor = UIColor.black.cgColor
This code seems to be working fine for me though, setting the backgroundColor to clear fix this
textField.backgroundColor = .clear

Underline UITextField does not work in StackView

I'm having multiple textfields for sign-up page and I have used stack view in it. When I try to assign underline colour to my UITextField it does not work. But if I do the same w/o StackView then it works fine.
extension UITextField {
func underlined() {
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = themeColor.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
nameTextField.underlined()
ageTextField.underlined()

Swift UITextField Border bottom and shadow issues

I have a UITextField that must have a bottom border. Since autolayout use my code is this:
override func viewDidLayoutSubviews() {
let border = CALayer()
let width = CGFloat(0.5)
border.borderColor = UIColor.darkGray.withAlphaComponent(0.3).cgColor
border.frame = CGRect(x: 0, y: txtUsername.frame.size.height - width, width: txtUsername.frame.size.width, height: txtUsername.frame.size.height)
border.borderWidth = width
txtUsername.layer.addSublayer(border)
txtUsername.layer.masksToBounds = true
}
This, however, excludes the fact that when I touch the UITextField, this should create me a shadow. the code is this:
//ombra txtField
self.view.addSubview(txtUsername)
txtUsername.layer.shadowOffset = CGSize(width: 5, height: 10)
txtUsername.layer.shadowOpacity = 0.3
txtUsername.layer.shadowRadius = 10
txtUsername.layer.masksToBounds = false;
txtUsername.clipsToBounds = false
the one excludes the other. how do I fix?`
You can use CAShapeLayerShadow
func border(){
let border = CALayer()
let width = CGFloat(0.5)
border.borderColor = UIColor.darkGray.withAlphaComponent(1).cgColor
border.frame = CGRect(x: 0, y: CustomerTextBox.frame.size.height - width, width: CustomerTextBox.frame.size.width, height: CustomerTextBox.frame.size.height)
border.borderWidth = width
CustomerTextBox.layer.addSublayer(border)
CustomerTextBox.layer.masksToBounds = true
}
func shadow(){
let Shape = CAShapeLayer()
let myPath = UIBezierPath(ovalIn: CustomerTextBox.frame)
Shape.shadowPath = myPath.cgPath
Shape.shadowColor = UIColor.lightGray.cgColor
Shape.shadowOffset = CGSize(width: 5, height: 3)
Shape.shadowRadius = 5
Shape.shadowOpacity = 0.8
view.layer.insertSublayer(Shape, at: 0)
}

How do I add a bottom and top border to UITextView?

let bottomBorder = CALayer()
let borderWidth = CGFloat(1.0)
bottomBorder.borderColor = UIColor(hex: 0xf5f5f5).CGColor
bottomBorder.frame = CGRect(x: 0, y: summary.frame.size.height - borderWidth, width: summary.frame.size.width, height: summary.frame.size.height)
bottomBorder.borderWidth = borderWidth
self.summary.layer.addSublayer(bottomBorder)
self.summary.layer.masksToBounds = true
I know how to add a bottom border, but when I apply the same principle to "topBorder", one of them disappears.
You can add top and bottom boarder using following code. you code have just framing issue.
let topBorder = CALayer()
let topHeight = CGFloat(1.0)
topBorder.borderColor = UIColor.blackColor().CGColor
topBorder.frame = CGRect(x: 0, y: 0, width: summary.frame.size.width, height: topHeight)
topBorder.borderWidth = summary.frame.size.width
self.summary.layer.addSublayer(topBorder)
let bottomBorder = CALayer()
let borderHeight = CGFloat(1.0)
bottomBorder.borderColor = UIColor.blackColor().CGColor
bottomBorder.frame = CGRect(x: 0, y: summary.frame.size.height - borderHeight, width: summary.frame.size.width, height: borderHeight)
bottomBorder.borderWidth = summary.frame.size.width
self.summary.layer.addSublayer(bottomBorder)
self.summary.layer.masksToBounds = true
I think it's your frames, review and modify this as necessary:
*Note this function was added as an extension to UITextView
func addBorders(color:UIColor) {
let bottomBorder = CALayer()
bottomBorder.backgroundColor = color.CGColor
bottomBorder.frame = CGRectMake(0, CGRectGetHeight(bounds) - 1, CGRectGetWidth(bounds), 1)
bottomBorder.name = "BottomBorder"
layer.addSublayer(bottomBorder)
let topBorder = CALayer()
topBorder.backgroundColor = color.CGColor
topBorder.frame = CGRectMake(0, 0, CGRectGetWidth(bounds), 1)
topBorder.name = "TopBorder"
layer.addSublayer(topBorder)
}
var bottomBorder = CALayer()
bottomBorder.frame = CGRect(x:0.0, y:self.descriptionTextView.frame.size.height - 1, width:self.descriptionTextView.frame.size.width, height: 1.0)
bottomBorder.backgroundColor = UIColor(hex:K.NODD_RED_HEX).cgColor
self.descriptionTextView.layer.addSublayer(bottomBorder)

Resources