文本在文本字段中不可见(Text not visible in text field)

编程入门 行业动态 更新时间:2024-10-20 20:43:18
文本在文本字段中不可见(Text not visible in text field)

我的导航栏中有一个按钮,当按下该按钮时,会调用一个函数(showAddItemtextField),该函数在导航栏中将文本字段显示为titleView。

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(showAddItemTextField))

到目前为止还可以,我可以在文本字段中键入文本(我知道因为当我点击文本字段时占位符文本消失,当我输入时我得到自动完成建议),但我键入的文本是不可见的。

到目前为止,这是我的代码:

// Show text field upon add button press lazy var addItemTextField: UITextField = { let textField = UITextField() textField.placeholder = "Add a new item..." return textField }() func showAddItemTextField() { self.navigationItem.titleView = addItemTextField }

我不确定问题是否是因为新的文本字段,作为navigationItem.titleView,通常存在(navigationItem.title)。 当文本字段存在时,我是否必须隐藏navigationItem.title? 或者是一个不同的问题?

我还应该补充一点,当我点击文本字段时,屏幕键盘不会出现。

I have a button in my navigation bar which, when pressed, calls a function (showAddItemtextField) that shows a text field as a titleView in the navigation bar.

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(showAddItemTextField))

That much is ok so far, and I can type text into the text field (I know because when I tap on the text field the placeholder text disappears, and when I type I get autocomplete suggestions), but the text I type is otherwise invisible.

Here's my code so far:

// Show text field upon add button press lazy var addItemTextField: UITextField = { let textField = UITextField() textField.placeholder = "Add a new item..." return textField }() func showAddItemTextField() { self.navigationItem.titleView = addItemTextField }

I'm not sure if the issue is because the new text field, as a navigationItem.titleView, goes on top of what is usually there (navigationItem.title). Do I have to hide navigationItem.title when the text field is present? Or is a different problem?

I should also add that the onscreen keyboard does not appear when I tap on the text field.

最满意答案

问题是您的文本字段没有大小。 当你创建它时,你没有给它一个大小,当你说:

let textField = UITextField()

然后你通过不必要的textField.translatesAutoresizingMaskIntoConstraints行来复杂化问题。

这对我来说很好:

let tf = UITextField(frame:CGRect(origin:.zero, size:CGSize(width:100, height:28))) tf.borderStyle = .bezel self.navigationItem.titleView = tf

我不是说那是你想要的尺寸/外观,但是它可以输入一个可见的文本字段。

The problem is that your text field has no size. You failed to give it a size when you created it, when you said:

let textField = UITextField()

Then you compounded the problem by the unnecessary textField.translatesAutoresizingMaskIntoConstraints line.

This works fine for me:

let tf = UITextField(frame:CGRect(origin:.zero, size:CGSize(width:100, height:28))) tf.borderStyle = .bezel self.navigationItem.titleView = tf

I'm not saying that that's the size / look you want, but it makes a visible text field you can type into.

更多推荐

本文发布于:2023-08-07 05:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   字段   中不   Text   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!