gpt4 book ai didi

ios - Swift - UITextField 以编程方式无法输入文本

转载 作者:行者123 更新时间:2023-11-30 12:58:55 32 4
gpt4 key购买 nike

我是 Swift 编程新手,我正在尝试构建一个包含 UITextField 的应用程序,但我遇到了问题,我创建了一个 UITextField以编程方式但似乎我无法在其中输入文本。这是我的 viewDidLoad

代码:

override func viewDidLoad() {
super.viewDidLoad()
self.tableView = UITableView(frame: self.tableView.frame, style: .Grouped)

self.groupInfoView.frame = CGRectMake(0, 44, self.view.frame.width, 100)
self.groupInfoView.backgroundColor = UIColor.orangeColor()
self.groupInfoView.userInteractionEnabled = true



groupImg.frame = CGRectMake(10, 50, 40, 40)
groupImg.layer.cornerRadius = 5
groupImg.layer.masksToBounds = true
groupImg.image = UIImage(named: "group-def-img")

groupTitle = UITextField.init(frame:CGRectMake(groupImg.frame.maxX + 5, 50, self.groupInfoView.frame.width-65, 40))
groupTitle.backgroundColor = UIColor.whiteColor()
groupTitle.layer.cornerRadius = 5
groupTitle.layer.masksToBounds = true
groupTitle.placeholder = "Enter Group Title"
groupTitle.textColor = UIColor.orangeColor()
groupTitle.textAlignment = .Center
groupTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18)
groupTitle.autocorrectionType = UITextAutocorrectionType.No
groupTitle.keyboardType = UIKeyboardType.Default
groupTitle.returnKeyType = UIReturnKeyType.Done
groupTitle.clearButtonMode = UITextFieldViewMode.WhileEditing;
groupTitle.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
groupTitle.enabled = true
groupTitle.userInteractionEnabled = true
groupTitle.delegate = self
groupTitle.text = "asda"


recipientLabel.frame = CGRectMake(5, 0, self.view.frame.width - 20, 40)
recipientLabel.font = UIFont(name: "HelveticaNeue-Light", size: 15)
recipientLabel.numberOfLines = 3
recipientLabel.textColor = UIColor.orangeColor()
recipientLabel.backgroundColor = UIColor.whiteColor()
recipientLabel.center.x = self.view.center.x
recipientLabel.layer.cornerRadius = 7
recipientLabel.layer.masksToBounds = true
recipientLabel.text = " To:"

self.groupInfoView.addSubview(groupTitle)
self.groupInfoView.addSubview(recipientLabel)
self.groupInfoView.addSubview(groupImg)
searchBar.delegate = self
self.titleLabel.text = "New Group Message"
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.tabBarController?.tabBar.barStyle = .Black
self.navigationController!.navigationBar.translucent = false
self.navigationController!.navigationBar.barTintColor = UIColor.orangeColor()

tableView.separatorInset = UIEdgeInsets(top: 0, left: 65, bottom: 0, right: 0)

tableView.registerClass(UserCell.self, forCellReuseIdentifier: cellId)

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(handleCancel))

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .Done, target: self, action: #selector(handleDone))


fetchUser()
self.tableView.allowsMultipleSelection = true

if let navigationBar = self.navigationController?.navigationBar {

let firstFrame = CGRectMake(0, -5, (self.navigationController?.navigationBar.frame.width)!, 40)
let secondFrame = CGRectMake(0, 17, (self.navigationController?.navigationBar.frame.width)!, 30)

titleLabel.frame = firstFrame
titleLabel.textColor = UIColor.whiteColor()
titleLabel.font = UIFont(name: "HelveticaNeue-SemiBold", size: 18)
titleLabel.textAlignment = .Center


countLabel.textColor = UIColor.whiteColor()
countLabel.font = UIFont(name: "HelveticaNeue-Light", size: 13)
countLabel.frame = secondFrame
countLabel.textAlignment = .Center

navigationBar.addSubview(groupInfoView)
navigationBar.addSubview(titleLabel)
navigationBar.addSubview(countLabel)

}

And this is my references:

Code:

var titleLabel = UILabel()
var countLabel = UILabel()
var recipientLabel = UILabel()
var groupImg = UIImageView()
var groupTitle: UITextField = UITextField()
var groupInfoView = UIView()

最佳答案

在全局范围内声明它,以便您可以在任何地方访问它。并声明其委托(delegate) UITextFieldDelegate Headerimageview 是我的 heartimage,而 HeaderView 是我的简单标题 View 。

@IBOutlet var Headerimageview: UIImageView!
@IBOutlet var HeaderView: UIView!
var groupTitle : UITextField = UITextField()

viewDidLoad()

groupTitle = UITextField.init(frame: CGRectMake(Headerimageview.frame.size.width + 25, 5, self.view.frame.width-85, 40))
groupTitle.backgroundColor = UIColor.whiteColor()
groupTitle.layer.cornerRadius = 5
groupTitle.layer.borderWidth = 1.0
groupTitle.layer.borderColor = UIColor.brownColor().CGColor
groupTitle.layer.masksToBounds = true
groupTitle.placeholder = "Enter Group Title"
groupTitle.textColor = UIColor.orangeColor()
groupTitle.textAlignment = .Center
groupTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18)
groupTitle.autocorrectionType = UITextAutocorrectionType.No
groupTitle.keyboardType = UIKeyboardType.Default
groupTitle.returnKeyType = UIReturnKeyType.Done
groupTitle.clearButtonMode = UITextFieldViewMode.WhileEditing
groupTitle.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
groupTitle.enabled = true
groupTitle.userInteractionEnabled = true
groupTitle.delegate = self
self.HeaderView.addSubview(groupTitle)

输出:

TextField 应该开始编辑名为的方法

TextField 确实开始编辑名为的方法

输入字符时调用此方法

输入字符时调用此方法

输入字符时调用此方法

TextField 应返回名为的方法

TextField 应该调用编辑方法

TextField 确实结束了名为的编辑方法

用我的代码更新你的代码...

 groupTitle = UITextField.init(frame:CGRectMake(groupImg.frame.maxX + 5, 25, self.groupInfoView.frame.width-65, 40))
groupTitle = UITextField.init(frame: CGRectMake(groupImg.frame.size.width + 25, 25, self.view.frame.width-85, 40))
self.groupInfoView.addSubview(groupTitle)
self.groupInfoView.addSubview(groupImg)
self.view.addSubview(groupInfoView)

关于ios - Swift - UITextField 以编程方式无法输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40078472/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com