繁体   English   中英

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

[英]Swift - UITextField Programmatically Cannot enter text

我是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()

全局声明它,以便您可以在任何地方访问它。 并声明其委托人UITextFieldDelegate Headerimageview是我的heartimage,HeaderView是我的简单标题视图。

@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应该snd编辑方法称为

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)

暂无
暂无

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

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