1

I want to create a rounded corner "Oval shape" UITextField. I have a problem with the inner border is not rounded and give strange log to the UITextField when the background of the UITextField same as the view

How border looks:

 how border look like

Image from the MockAUp what I want to achieve:

Image from the MockAUp what I want to achieve

Swift code:

txtfEmail.layer.cornerRadius = 26
txtfEmail.clipsToBounds = true
txtfEmail.attributedPlaceholder =  NSAttributedString(string: "Email",
                                                      attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)])

email UITextfield inspectere

enter image description here

4
  • Show the code you used to add the border. Commented Apr 18, 2019 at 17:48
  • I don't have any code to add border Commented Apr 18, 2019 at 17:54
  • Then why is there a border in the first picture? Commented Apr 18, 2019 at 17:56
  • I don't know where is that annoying border set I am sure is not from controller code Commented Apr 18, 2019 at 18:13

3 Answers 3

1

One approach would be to create a custom UIView class with an embedded text field.

Here's an example, using @IBInspectable and @IBDesignable to let you see it during Storyboard design:

import UIKit

@IBDesignable
class RoundedTextField: UIView {

    @IBInspectable var placeholder: String = "" {
        didSet {
            textField.attributedPlaceholder = NSAttributedString(string: placeholder,
                                                                 attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)])
        }
    }

    let textField: UITextField = {
        let v = UITextField()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    override func prepareForInterfaceBuilder() {
        commonInit()
    }

    func commonInit() -> Void {

        layer.borderColor = UIColor.white.cgColor
        layer.borderWidth = 2
        layer.masksToBounds = true

        addSubview(textField)

        NSLayoutConstraint.activate([
            textField.topAnchor.constraint(equalTo: topAnchor, constant: 12.0),
            textField.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12.0),
            textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20.0),
            textField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20.0),
            ])

    }

    override func layoutSubviews() {
        super.layoutSubviews()
        layer.cornerRadius = bounds.size.height * 0.5
    }

}

Result in Storyboard / Interface Builder:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

try this it works for me open storyboard drag and drop textfield ctrl click and create an outlet in view controller and name it lets say emailfield then write this code

@iboultet weak var emailfield: uitextfield!{
didset{
emailfield.layer.masktobounds = true
emailfield.layer.cornerradius = 26
emailfield.layer.borderwidth = 1
emailfield.backgroundcolor = whatever color you want

Comments

0

You should create the rounded border like this:

txtfEmail.layer.cornerRadius = txtfEmail.height / 2
txtfEmail.layer.borderWidth = 1
txtfEmail.layer.borderColor = UIColor.black.cgColor

Looks like the border is set to the view under the text field

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.