简体   繁体   中英

Adding multiple sublayers to UIView will only display one

Trying to add a bottom border to my login system, I have used the following code:

var bottomBorder = CALayer()
bottomBorder.frame = CGRectMake(0.0, userEmailAddressTextField.frame.size.height - 1, userEmailAddressTextField.frame.size.width, 1.0)
bottomBorder.backgroundColor = UIColor.blackColor().CGColor
userEmailAddressTextField.layer.addSublayer(bottomBorder)

I am trying to do this to my password textfield as well. I have used a various amount of techniques but none seem to work.

First attempt: Adding the same code again, this only shows the bottom border for the second bar.

Second attempt: I tried the same thing with a different variable being bottomBordertwo and then replicating code, but that wouldn't work either. Any solutions?

Create a common function

func addBottomLayerToTheView(view:UIView)
{
    var bottomBorder = CALayer()
    bottomBorder.frame = CGRectMake(0.0, view.frame.size.height - 1, view.frame.size.width, 1.0)
    bottomBorder.backgroundColor = UIColor.blackColor().CGColor
    view.layer.addSublayer(bottomBorder)
}

call this function after creating textFields or any other views that need bottom layer like:

In viewDidLoad()

addBottomLayerToTheView(textField)

addBottomLayerToTheView(textField2)

addBottomLayerToTheView(anyView)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM