【问题标题】:Xcode Swift programatically set UITextfield X position to centerXcode Swift 以编程方式将 UITextfield X 位置设置为中心
【发布时间】:2020-01-12 23:34:59
【问题描述】:

我试图将我的文本字段的 X 位置居中,使其位于我的 pageviewcontroller 的中心。 这就是我现在的编程方式:

  lastnamefield =  UITextField(frame: CGRect(x: 100.33, y:431.33 , width: 206, height: 34))
        lastnamefield.backgroundColor = .clear
        lastnamefield.placeholder = "Last name"
        lastnamefield.textColor = .black
        lastnamefield.font = UIFont(name: "Futura", size: 20)
        lastnamefield.borderStyle = UITextField.BorderStyle.roundedRect
          lastnamefield.autocorrectionType = UITextAutocorrectionType.no
          lastnamefield.keyboardType = UIKeyboardType.default
          lastnamefield.returnKeyType = UIReturnKeyType.done
          lastnamefield.clearButtonMode = UITextField.ViewMode.whileEditing
          lastnamefield.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
      //  lastnamefield.delegate = self as! UITextFieldDelegate
          self.view.addSubview(lastnamefield)






    emailfield =  UITextField(frame: CGRect(x: 100.33, y:488 , width: 206, height: 34))
        emailfield.backgroundColor = .clear
        emailfield.placeholder = "Email Adress"
        emailfield.textColor = .black
        emailfield.font = UIFont(name: "Futura", size: 20)
        emailfield.borderStyle = UITextField.BorderStyle.roundedRect
          emailfield.autocorrectionType = UITextAutocorrectionType.no
          emailfield.keyboardType = UIKeyboardType.default
          emailfield.returnKeyType = UIReturnKeyType.done
          emailfield.clearButtonMode = UITextField.ViewMode.whileEditing
          emailfield.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
      //  emailfield.delegate = self as! UITextFieldDelegate
          self.view.addSubview(emailfield)

我知道这显然不会居中,但我已经尝试过这样的事情:

emailfield.widthAnchor.constraint(equalTo: revealingSplashView.widthAnchor, multiplier: 0.8).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
emailfield.heightAnchor.constraint(equalToConstant: 25).isActive = true
emailfield.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

我的应用程序要么崩溃,要么出错。我对这个约束有点陌生,我必须以编程方式进行,因为我的 pageviewcontroller 也是以编程方式进行的。 我为我糟糕的写作技巧道歉。我不是英国人。

【问题讨论】:

  • 请分享您的崩溃日志和错误消息。
  • 要将视图置于其超级视图的中心,您可以简单地尝试lastnamefield.center = view.center

标签: ios swift xcode constraints


【解决方案1】:

1- 这会崩溃

emailfield.delegate = self as! UITextFieldDelegate

改为

emailfield.delegate = self

并使vc符合

class VC:UIViewController,UITextFieldDelegate {

2-

添加约束时不要忘记设置

self.emailfield.translatesAutoresizingMaskIntoConstraints = false

3- 给你设置 width 、 height 和 centerX 的电子邮件,你还需要设置一个 y 约束 centerY 、 top 或 bottom

【讨论】:

  • 没有centerX和centerY这样的东西吗?我应该写什么?这不起作用:lastnamefield = UITextField(frame: CGRect(x: centerX, y:centerY, width: 206, height: 34))
  • 见我上面的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 2017-09-16
  • 2017-09-25
  • 2014-09-05
  • 2018-07-03
  • 1970-01-01
相关资源
最近更新 更多