Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

style iOS textfield borders

how can I style iOS textfield borders to be a specific color and width and have a transparent background

1 Answer

Hi,

you can use the CALayer property that is inherited from UIView. With that you can style your UITextField in many ways. You can also consider subclassing UITextField, if you need this TextField at many places.

Some Playground code in Swift:

import UIKit
import QuartzCore

let textField = UITextField(frame: CGRectMake(0.0, 0.0, 200.0, 44.0))
textField.text = "Test"
textField.layer.cornerRadius = 8.0
textField.layer.masksToBounds = true
textField.layer.borderColor = UIColor.blueColor().CGColor
textField.layer.borderWidth = 2.0
textField

let view = UIView(frame: CGRectMake(0, 0, 250, 100))
view.addSubview(textField)
view.backgroundColor = UIColor.redColor()

view