【发布时间】:2016-06-30 05:07:20
【问题描述】:
我有一个以编程方式创建的 UITextField。我尝试使用UIScreen.mainScreen().bounds.width / 2 将其居中,但似乎不对。使用 CGRect 对齐 ui 项的正确方法是什么?如何将 UITextField 水平居中?
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
let textFieldFrame = CGRect(x: screenWidth / 2, y: screenHeight / 2, width: 200.00, height: 40.00)
let textField = HoshiTextField(frame: textFieldFrame)
textField.placeholderColor = UIColor.whiteColor()
textField.borderActiveColor = UIColor.whiteColor()
self.view.addSubview(textField)
我还有一个问题。我有 @IBOutlet weak var label: UILabel! 并一起水平对齐上面的 UITextField,我也想给它一个约束以保持它与 UILabel 的距离。
| --Label-- |
| | | I want to center UITextfield
| | 20px | horizontally and give it a constraint
| | | to stay 20px below Label
| UITextField |
【问题讨论】:
标签: ios xcode swift uiview storyboard