如何将两个UITextField与Swift一起设置?

时间:2016-08-17 12:48:47

标签: ios swift uitextfield

我正在尝试将两个UITextFieldsSwift放在一起,类似于这个问题:UITextField Set Border

所以结果应该是一个UITextField高于另一个,并且它们会在第一个的边界底部折叠。

我正在关注相关问题的代码:

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.

我也尝试过接受的答案,但它不识别该属性(我尝试使用borderStyle,但它也无效,因为我无法将其设置为UITextBorderStyleNone)。

我现在得到的结果是:

enter image description here

我错过了什么吗?我该怎么办才能把它们组合在一起?

提前致谢!

4 个答案:

答案 0 :(得分:0)

在Swift中设置UITextBorderStyleNone:

yourTextField.borderStyle = .None

答案 1 :(得分:0)

我认为您有3个解决方案并且都使用UITextBorderStyleNon‌e

  1. 添加2张背景图片
  2. 制作分组UITableView,单元格应包含UITextFields
  3. 在frameRect方法中绘制
  4. 我建议使用二号

答案 2 :(得分:0)

为您的概念

<强>步骤-1

创建一个UIView并添加UIVIew的两个文本字段子视图。

<强>步骤-2

yourTextField.borderStyle设置为None并设置边框宽度和边框颜色

<强>步骤-3

将UIView的圆角半径设置为UIView.layer.cornerRadius = 5

更新回答

您可以在here

下载示例项目

创建一个UIVIew和两个文本字段并设置yourTextField.borderStyle = .None

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

并在您的网页中调用

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
}

你得到的输出为

enter image description here

答案 3 :(得分:0)

实现这一目标的最佳方式..看看下面的代码

@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"
    }
}

或者您可以用最好的方式进行编辑..

相关问题