简体   繁体   English

Swift UITextField没有显示

[英]Swift UITextField not showing up

I just started working on an app and am trying to dynamically add labels and textfields to my view controller's main view, but the textfields just don't show up. 我刚刚开始开发应用程序,并试图将标签和文本字段动态添加到视图控制器的主视图中,但是这些文本字段只是不显示。 I have tried the code below, as well as putting textfields into my storyboard, grabbing them via their tags and then trying to change their frames, but when I try it that way, the frames don't change. 我尝试了下面的代码,并将文本字段放入情节提要中,通过它们的标签抓取它们,然后尝试更改其框架,但是当我这样尝试时,框架不会改变。 I can still tap on the invisible text fields and type in them, but there is no indication that they are there. 我仍然可以点击不可见的文本字段并输入它们,但是没有迹象表明它们在那里。 I feel like it is something really simple that I am just missing. 我觉得我真的很想念它。 Thanks! 谢谢!

All of the code below is in viewDidLoad just after the super.viewDidLoad() call 以下所有代码均在super.viewDidLoad()调用之后位于viewDidLoad

let WIDTH = self.view.frame.size.width
let HEIGHT = self.view.frame.size.height
let PADDING:CGFloat = 10
let MAX_PLAYERS = 6
let NAME_X = 2 * PADDING
let FIELD_X = WIDTH / 3
let START_Y:CGFloat = 50
let PLAYER_HEIGHT:CGFloat = 40

for i in 0..<MAX_PLAYERS
{
    let name = UILabel()
    name.text = "Player \(i+1):"
    name.sizeToFit()
    name.frame.origin = CGPointMake(NAME_X, START_Y + (CGFloat(i) * PLAYER_HEIGHT))
    self.view.addSubview(name)

    let fieldFrame = CGRectMake(FIELD_X, START_Y + (CGFloat(i) * PLAYER_HEIGHT), (WIDTH - FIELD_X) - (2 * PADDING), 40)
    let field = UITextField(frame:fieldFrame)
    field.frame = fieldFrame
    field.hidden = false
    self.view.addSubview(field)
}

Try this. 尝试这个。 Turn on view debugging. 打开视图调试。 Run the project. 运行项目。 Go to Xcode > Debug > View Debugging > Show view frames. 转到Xcode>调试>视图调试>显示视图框架。 I am typing this on the go so the wording may not be exact. 我在旅途中键入此内容,因此措辞可能不准确。 But try this to see how your controls are sized. 但是,尝试此操作以查看控件的大小。

Also, setting borderStyle of the text fields. 另外,设置文本字段的borderStyle。 By default, no borders are shown. 默认情况下,不显示任何边框。

Try setting the font property of the UILabel and the UITextField 尝试设置UILabel和UITextField的font属性

myLabel.font

The font property takes a UIFont object font属性带有一个UIFont对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM