I'm new to Swift programming, I'm trying to build an app with UITextField
in it, But I'm having a problem, I created an UITextField
programmatically but it seems like I cannot enter a text in it. Here's my viewDidLoad
Code:
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()
Declare its globally so you can access it anywhere. and declare its delegates UITextFieldDelegate
Headerimageview is my heartimage and HeaderView is my simple header view.
@IBOutlet var Headerimageview: UIImageView!
@IBOutlet var HeaderView: UIView!
var groupTitle : UITextField = UITextField()
In 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)
Output :
TextField should begin editing method called
TextField did begin editing method called
While entering the characters this method gets called
While entering the characters this method gets called
While entering the characters this method gets called
TextField should return method called
TextField should snd editing method called
TextField did end editing method called
Update your this code with mine one...
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.