0

I am trying to put two UITextFields with Swift together similar to this question: UITextField Set Border.

So the result should be one UITextField above the other and they will collapse on the border-bottom of the first one.

I am following the code that is on the question related:

tfUser.layer.borderWidth = 1
tfUser.layer.borderColor = UIColor(red: 69/255, green: 106/255, blue: 153/255, alpha: 1).CGColor
tfUser.layer.cornerRadius = 5
tfUser.clipsToBounds = true; //I am not able to equal this property to 'YES' so I put 'true'. Looking on the documentation I could see that it only accepts a boolean value.

tfPassword.layer.borderWidth = 2
tfPassword.layer.borderColor = UIColor(red: 69/255, green: 106/255, blue: 153/255, alpha: 1).CGColor
tfPassword.layer.cornerRadius = 5
tfPassword.clipsToBounds = true; //I am not able to equal this property to 'YES' so I put 'true'. Looking on the documentation I could see that it only accepts a boolean value.

I also have tried with the accepted answer but it does not recognize that property (I tried with borderStyle but it also does not work as I cannot set it to UITextBorderStyleNone).

The result I am getting right now is:

enter image description here

Am I missing something? What can I do to put them together?

Thanks in advance!

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

4 Answers4

0

To set UITextBorderStyleNone in Swift:

yourTextField.borderStyle = .None
Igor
  • 12,165
  • 4
  • 57
  • 73
0

I think you have 3 solutions and both use UITextBorderStyleNon‌e:

  1. add 2 background image
  2. make grouped UITableView, cells should contain UITextFields
  3. Draw it in frameRect method

I recommend to use number two

Sergey Petruk
  • 781
  • 7
  • 13
0

for your concept

step-1

Create One UIView and add the two textfield subview of UIVIew.

Step-2

set your yourTextField.borderStyle as None and set border width and border color

step-3

set the corner radius of UIView as UIView.layer.cornerRadius = 5

updated answer

you can downloaded the sample project in here

create one UIVIew, and two textfields and set your yourTextField.borderStyle = .None

  @IBOutlet weak var backview: UIView!
  @IBOutlet weak var txtUserName: UITextField!
  @IBOutlet weak var txtpassword: UITextField!

and called in your page like

override func viewDidLoad() {
    super.viewDidLoad()

   self.changecornerRadius(self.txtpassword)
  self.changecornerRadius(self.txtUserName)
self.changecornerRadiussd(self.backview)
}



func  changecornerRadius(textfield: UITextField)  {
    textfield.layer.borderWidth = 1
    textfield.layer.borderColor = UIColor.lightGrayColor().CGColor
    textfield.layer.cornerRadius = 2
    textfield.clipsToBounds = true
     let paddingView = UIView(frame:CGRectMake(0, 0, 30, 30))
    textfield.leftView=paddingView;
    textfield.leftViewMode = .Always
}

func  changecornerRadiussd(currentView: UIView)  {
    currentView.layer.borderWidth = 1
    currentView.layer.borderColor = UIColor.blueColor().CGColor
    currentView.layer.cornerRadius = 5
    currentView.clipsToBounds = true
}

you get the output as

enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • see this link also http://stackoverflow.com/questions/31640488/how-do-i-create-connected-text-fields-in-ios – Anbu.Karthik Aug 17 '16 at 13:03
  • Choice 2 does not produce the same result as shown in the link OP mentioned. You may still have to work on setting the cornerRadius – GenieWanted Aug 17 '16 at 13:03
  • Ok. I have tried with your code and it gives to me a space between two `UITextFields` and also another space between the second `UITextField`. I also downloaded your project and I saw that you do not have anything on your visual interface on your `Main.storyboard`. Are you declaring them in another file? I am totally new so I think I am missing something basic concept. – Francisco Romero Aug 18 '16 at 07:35
  • @Error404 - no no I added the textfield in ViewController, Its available in `Main.storyboard` if it is not visible change the background color of View Contrller and check once – Anbu.Karthik Aug 18 '16 at 07:38
  • I have changed background color to the TextView and a text to see if on `Main.storyboard` (on ViewController) it is changed but I cannot see any effect on the visual interface (I can see the changes only if I run the code). But I noticed that it is declared in the tree that is on the left of the `Main.storyboard`. In your example, in mine I cannot figure out what could be the problem because the code to do this `UIView` is the same. – Francisco Romero Aug 18 '16 at 08:22
  • @Error404 - I added the background color everything in the code , – Anbu.Karthik Aug 18 '16 at 08:52
  • I could get both `UITextView` together but adjusting them visually, not programatically. It is why I wanted to see how they were adjusted on your visual interface but I could not see them neither with background. – Francisco Romero Aug 18 '16 at 08:54
  • @Error404 - shall I delete my answer if it is not useful – Anbu.Karthik Aug 18 '16 at 11:26
  • I do not think that is not useful at all because if I execute your project it works like a charm. Just I am not able to detect what are the differences between my project and yours and finally I adapted it visually instead of programatically. I will accept your answer because I think it is the most relevant of all the answers here (and helped the fact that I did not know about UIView). – Francisco Romero Aug 18 '16 at 11:29
  • @Error404 - sure bro , if you need the support related to this I will help – Anbu.Karthik Aug 18 '16 at 11:31
0

the best way to achieve this ..have a look below code

@IBAction func showLoginPasswordAlert(sender: UIButton) {

    var alert = UIAlertController(title: "User login", message: "login password ..!!", preferredStyle:
        UIAlertControllerStyle.Alert)

    alert.addTextFieldWithConfigurationHandler(configurationTextFieldLogin)
    alert.addTextFieldWithConfigurationHandler(configurationTextFieldPassword)

    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
        print("User click Ok button")
//            print(self.textField.text)
    }))

    self.presentViewController(alert, animated: true, completion: {
        print("completion block")
    })
}

// func to add login textfield
func configurationTextFieldLogin(textField: UITextField!)
{
    if let aTextField = textField {
        textField.text = "Login"
    }
}

// function to add password textfield
func configurationTextFieldPassword(textField: UITextField!)
{
    if let aTextField = textField {
        textField.text = "Password"
    }
}

or you can edit in your best way..

vaibhav
  • 4,038
  • 1
  • 21
  • 51